Created
October 1, 2021 21:57
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