Skip to content

Instantly share code, notes, and snippets.

@twillis
Last active December 29, 2015 02:59
Show Gist options
  • Save twillis/7604032 to your computer and use it in GitHub Desktop.
Save twillis/7604032 to your computer and use it in GitHub Desktop.
import sys
import random
from timeit import timeit
try:
LISTSIZE = int(sys.argv[1])
except IndexError:
LISTSIZE = 100
SETUP = "import random;LISTSIZE=%s;expected_range = range(LISTSIZE);mydata = expected_range[:];mydata.pop(random.choice(xrange(1, len(mydata))))" % LISTSIZE
TRIES = 10
print "Toms(%s): " % LISTSIZE, timeit("set(expected_range) - set(mydata)", setup=SETUP, number=TRIES)
print "Francesco(%s): " % LISTSIZE, timeit("n=len(mydata);expected_sum=n*(n+1)/2;real_sum = sum(mydata);expected_sum-real_sum", setup=SETUP, number=TRIES)
print "Artem(%s): " % LISTSIZE, timeit("sum(xrange(1,%s)) - sum(mydata)" % LISTSIZE, setup=SETUP, number=TRIES)
$ python linkedin_timeit.py 1000000
Toms(1000000): 0.975708007812
Francesco(1000000): 0.0880701541901
Artem(1000000): 0.178301095963
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment