Skip to content

Instantly share code, notes, and snippets.

@juanarrivillaga
Created August 29, 2021 20:54
Show Gist options
  • Save juanarrivillaga/d5a0c2e0d44ad27171bc861d789aaae7 to your computer and use it in GitHub Desktop.
Save juanarrivillaga/d5a0c2e0d44ad27171bc861d789aaae7 to your computer and use it in GitHub Desktop.
Timing comparisons for creating a tuple from a generator expression versus a list comprehension
import pandas as pd
import matplotlib.pyplot as plt
from timeit import timeit
from tqdm import tqdm
gen_exp = "tuple(x for x in range({}))"
list_comp = "tuple([x for x in range({})])"
N = range(1, 500_001, 5000)
times_gen = [
timeit(gen_exp.format(n), number=50) for n in tqdm(N)
]
times_list_comp = [
timeit(list_comp.format(n), number=50) for n in tqdm(N)
]
pd.DataFrame(
dict(generator_expression=times_gen, list_comp=times_list_comp), index=N
).plot()
plt.savefig('result.png')
@juanarrivillaga
Copy link
Author

result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment