Skip to content

Instantly share code, notes, and snippets.

@joseberlines
Last active November 10, 2021 23:09
Show Gist options
  • Save joseberlines/c3d83d35da806c9aca3e0bfb04b5b325 to your computer and use it in GitHub Desktop.
Save joseberlines/c3d83d35da806c9aca3e0bfb04b5b325 to your computer and use it in GitHub Desktop.
times.py
import pandas as pd
# list of 1000
L=[{'name':'e'+str(i),
'explanation':'objection1',
'law':(),
'CS':('Barcelona','Vigo')} for i in range(1000)]
#%%timeit
next(item for item in L if item["name"] == "e500")
# 27.5 µs ± 351 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#%%timeit
[d for d in L if d['name']=='e500'][0]
# 55.9 µs ± 2.77 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
# Does it depend on which dictionary of the list I am calling?
#%%timeit
next(item for item in L if item["name"] == "e500")
# 27.7 µs ± 272 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#%%timeit
[d for d in L if d['name']=='e500'][0]
# 59.8 µs ± 5.02 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#%%timeit
next(item for item in L if item["name"] == "e900")
# 52.7 µs ± 3.79 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#%%timeit
[d for d in L if d['name']=='e900'][0]
# 67 µs ± 6.09 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
df = pd.DataFrame(L).set_index('name')
#%%timeit
df.loc[['e500']].reset_index().to_dict('records')
# 1.34 ms ± 155 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
#%%timeit
df.loc[['e900']].reset_index().to_dict('records')
# 1.28 ms ± 208 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment