Your example is not correct.
When using set in the first approach (using
the correct comparison between the two approaches would be:
When using set in the first approach (using
in
and set
) you measure also the conversion to setthe correct comparison between the two approaches would be:
from timeit import timeit print(timeit('target in spam', setup='spam = set(range(11, 1000)); target=11')) print(timeit('any(target == item for item in spam)', setup='spam = set(range(11, 1000)); target = 11'))
Output:0.02962722000006579
0.45690207600000576
from timeit import timeit print(timeit('target in spam', setup='spam = set(range(11, 1000)); target=999')) print(timeit('any(target == item for item in spam)', setup='spam = set(range(11, 1000)); target = 999'))
Output:0.046424419999993916
56.531857142000035
same result, with using list in both cases:from timeit import timeit print(timeit('target in spam', setup='spam = list(range(11, 1000)); target=11')) print(timeit('any(target == item for item in spam)', setup='spam = list(range(11, 1000)); target = 11'))
Output:0.041607157000044026
0.4492151309998462
from timeit import timeit print(timeit('target in spam', setup='spam = list(range(11, 1000)); target=999')) print(timeit('any(target == item for item in spam)', setup='spam = list(range(11, 1000)); target = 999'))
Output:11.636881140000014
49.844653193999875
just the set conversion, i.e. how much you "add" in your set examples:from timeit import timeit print(timeit('set(spam)', setup='spam = list(range(11, 1000))'))
Output:13.877401454999927
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs