Skip to content

Instantly share code, notes, and snippets.

@Scinawa
Created January 16, 2016 11:39
Show Gist options
  • Save Scinawa/485985f589410fc238a0 to your computer and use it in GitHub Desktop.
Save Scinawa/485985f589410fc238a0 to your computer and use it in GitHub Desktop.
from datetime import datetime
def with_list(iterable):
t0 = datetime.now()
first, rest = iterable[0], iterable[1:]
t1 = datetime.now()
return (t1-t0).total_seconds()
def with_extended(iterable):
t0 = datetime.now()
first, *rest = iterable
t1 = datetime.now()
return (t1-t0).total_seconds()
print("calculating average with range()")
print("AVG of\t list sl\t extended")
for i in range(2,7):
lists = [with_list(range(1, 10**i)) for _ in range(50) ]
extend = [with_extended(range(1, 10**i)) for _ in range(50) ]
print("10**%d\t %.5f \t %.5f"% (i, sum(lists)/len(lists), sum(extend)/len(extend)))
print("\ncalculating average with list(range())")
print("AVG \t list sl\t extended")
for i in range(2,7):
lists = [with_list(list(range(1, 10**i))) for _ in range(50) ]
extend = [with_extended(list(range(1, 10**i))) for _ in range(50) ]
print("10**%d\t %.5f \t %.5f"% (i, sum(lists)/len(lists), sum(extend)/len(extend)))
"""" OUTPUT
calculating average with range()
AVG of list sl extended
10**2 0.00002 0.00002
10**3 0.00002 0.00006│
10**4 0.00002 0.00045│
10**5 0.00001 0.00863
10**6 0.00002 0.08700
calculating average with list(range())
AVG list sl extended
10**2 0.00002 0.00002
10**3 0.00003 0.00002
10**4 0.00010 0.00011
10**5 0.00110 0.00114
10**6 0.01114 0.02591
""""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment