Skip to content

Instantly share code, notes, and snippets.

@joseberlines
Last active November 10, 2021 23:12
Show Gist options
  • Save joseberlines/7d955af1705aef7bcc7302b551405f04 to your computer and use it in GitHub Desktop.
Save joseberlines/7d955af1705aef7bcc7302b551405f04 to your computer and use it in GitHub Desktop.
amillion.py
L=[{'name':'e'+str(i),
'explanation':'objection1',
'law':(),
'CS':('barcelona','Vigo')} for i in range(1000000)]
#%%timeit
next(item for item in L if item["name"] == "e300000")
# 22.3 ms ± 2.58 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
#%%timeit
[d for d in L if d['name']=='e300000'][0]
# 73.8 ms ± 9.35 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
#%%timeit
next(item for item in L if item["name"] == "e800000")
# 56.1 ms ± 4.42 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
#%%timeit
[d for d in L if d['name']=='e800000'][0]
# 73.3 ms ± 5.55 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
df = pd.DataFrame(L).set_index('name')
#%%timeit
df.loc[['e800000']].reset_index().to_dict('records')
# 1.71 ms ± 901 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment