Created
October 1, 2021 21:57
-
-
Save juanarrivillaga/c0910cee9f824eb2075f265fb63f238f to your computer and use it in GitHub Desktop.
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 matplotlib.pyplot as plt | |
import pandas as pd | |
from timeit import timeit | |
setup = """ | |
def list_comp(d): | |
return [ (x*2)/3 for x in d ] | |
def pre_allocate(d): | |
result = [None]*len(d) | |
for i, x in enumerate(d): | |
result[i] = (x*2)/3 | |
return result | |
d = list(range({})) | |
""" | |
lc = [timeit("list_comp(d)", setup=setup.format(n), number=10) for n in N] | |
pa = [timeit("pre_allocate(d)", setup=setup.format(n), number=10) for n in N] | |
pd.DataFrame(dict(pre_allocate=pa, list_comp=lc), index=N).plot() | |
plt.savefig("pre-allocate-vs-list-comp.png") |
Author
juanarrivillaga
commented
Oct 1, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment