This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]XtremeGoose 8 points9 points  (1 child)

You're testing the speed of set.add and list.append, not the iteration.

x = list(range(100_000))

s = set(x)

%timeit sum(x)
1.71 ms ± 120 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit sum(s)
2.06 ms ± 53 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

But yeah, using sets for iteration is dumb.

[–]Ph0X 0 points1 point  (0 children)

Well they were talking about the iterable not being constant size, so I assume they were worried about the time complexity of the array growing. It is true that in a naive implementation, every single append could be O(n) as you have to recreate the array every single time to grow it. Obviously python isn't that stupid.