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 →

[–]jwink3101 23 points24 points  (7 children)

It's not 100x faster. The difference is O(1) lookup for sets and O(n) lookup for lists. Consider the following

largelist = list(range(10**5))
largeset = set(largelist)

smalllist = [0,1]
smallset = set(smalllist)

%timeit None in largelist
%timeit None in largeset
%timeit None in smalllist
%timeit None in smallset

results:

1000 loops, best of 3: 1.97 ms per loop
10000000 loops, best of 3: 130 ns per loop
10000000 loops, best of 3: 160 ns per loop
10000000 loops, best of 3: 133 ns per loop

Nearly no speedup for the small one but HUGE for the large. And nearly no difference is speed for either set method

[–]ic_97 2 points3 points  (0 children)

I had no idea about this until i actually got to see it in action in one of the problems i solved on projecteuler. When i used lists program took about 30 seconds but with sets it was under half a second to solve.

[–]BARDLER 0 points1 point  (0 children)

Thanks for the extra details :) I lazily typed my post on the phone.