Created
August 29, 2021 20:54
-
-
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
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
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') |
Author
juanarrivillaga
commented
Aug 29, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment