3 ms·
Funny that Hack #1 compares list versus set value lookup, but the timer doesn't include the time to copy the list into a set. Hack #2 warns against unnecessary
by driggs 11mo ago
Funny that Hack #1 compares list versus set value lookup, but the timer doesn't include the time to copy the list into a set. Hack #2 warns against unnecessary copying, and the time for copying the list is almost the same as the performance gain in Hack #1.
In fact, creating a set takes longer than copying a list since it requires hash insertion, so it's actually much faster to do the opposite of what they suggest for #1 (in the case of a single lookup, for this test case).
Here's the results with `big_set = set(big_list)` inside the timing block for the set case:
List lookup: 0.013985s
Set lookup: 0.052468s
- godelski 11mo agoI'm surprised that this was not the first complaint. It's pretty clear "Hack" #1 isn't going to work. import random import time def timeit(func, _list, n=1000): start = time.time() for _ in range(n): func(_list=_list ) end = time.time() print(f"Took {end-start} s") return def lstsearch(_list): sf = random.randint(0,len(_list)) if sf in _list: return return def setsearch(_list): sf = random.randint(0, len(_list)) if sf in set(_list): return return mylist = list(range(100000)) timeit(lstsearch, mylist) timeit(setsearch, mylist) ---- Took 0.23349690437316895 s Took 0.8901607990264893 s