Skip to content

Instantly share code, notes, and snippets.

@xiantail
Created December 28, 2015 23:38
Show Gist options
  • Save xiantail/ef2fc241f9d4c8038402 to your computer and use it in GitHub Desktop.
Save xiantail/ef2fc241f9d4c8038402 to your computer and use it in GitHub Desktop.
Introducing Python / Chapter 12
from timeit import timeit
def make_list_1():
result = []
for value in range(1000):
result.append(value)
return result
def make_list_2():
result = [value for value in range(1000)]
return result
print('time_list_1 takes', timeit(make_list_1, number=1000), 'sec')
print('time_list_2 takes', timeit(make_list_2, number=1000), 'sec')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment