Created
January 19, 2022 00:03
-
-
Save axgkl/210595d51725008d12993d3ba43f20ed to your computer and use it in GitHub Desktop.
lists_vs_sets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Perf lists versus sets for indexed operations. | |
""" | |
import random | |
import time | |
rand = lambda i: random.randint(1, i) | |
now = time.time | |
I = 100 * 1000 | |
# create a big list and an anloge set: | |
a_list = [i for i in range(I)] | |
a_set = {i for i in range(I)} | |
# we'll remove from those these (every 10th item): | |
difflist = [i * 10 for i in range(int(I / 10))] | |
diffset = {i * 10 for i in range(int(I / 10))} | |
t0 = now() | |
for i in difflist: | |
if i in a_list: | |
a_list.remove(i) | |
dtl = now() - t0 | |
print('with lists the diff took', dtl) | |
remaining = len(a_list) | |
t0 = now() | |
a_set.difference_update(diffset) | |
assert len(a_set) == remaining # go sure we have the same result | |
dts = now() - t0 | |
print('with sets the diff took', dts) | |
print('=> sets are %s times faster' % int(dtl / dts)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python perf.py
with lists the diff took 9.214841842651367
with sets the diff took 0.0005748271942138672
=> sets are 16030 times faster