Skip to content

Instantly share code, notes, and snippets.

@StrikingLoo
Created August 20, 2018 00:22
Show Gist options
  • Select an option

  • Save StrikingLoo/202f7c6ffb88113e9fe8774c75d98a3d to your computer and use it in GitHub Desktop.

Select an option

Save StrikingLoo/202f7c6ffb88113e9fe8774c75d98a3d to your computer and use it in GitHub Desktop.
import time
BIG = 20000000
def f(k):
return 2*k
def list_a():
list_a = []
for i in range(BIG):
list_a.append(f(i))
def list_b():
list_b = [f(i) for i in range(BIG)]
def list_a_filtered():
list_a = []
for i in range(BIG):
if i%2==0:
list_a.append(f(i))
def list_b_filtered():
list_b = [f(i) for i in range(BIG) if i%2==0]
def benchmark(function, function_name):
start = time.time()
function()
end = time.time()
print("{0} seconds for {1}".format((end - start), function_name))
benchmark(list_a, "list a")
benchmark(list_b, "list b")
benchmark(list_a_filtered, "filtered list a")
benchmark(list_b_filtered, "filtered list b")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment