Skip to content

Instantly share code, notes, and snippets.

@ysinjab
Last active October 12, 2018 17:15
Show Gist options
  • Save ysinjab/a4a05e6e3eaca8fbdbc39f0f3d3fdeef to your computer and use it in GitHub Desktop.
Save ysinjab/a4a05e6e3eaca8fbdbc39f0f3d3fdeef to your computer and use it in GitHub Desktop.
def append_to_list(size=1000000):
result = []
for i in range(size):
result.append(1)
def fill_pre_allocated_list(size=1000000):
result = size * [None]
for i in range(size):
result[i]= 1
%timeit append_to_list()
# 10 loops, best of 3: 150 ms per loop
%timeit fill_pre_allocated_list()
# 10 loops, best of 3: 111 ms per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment