Last active
December 29, 2015 02:59
-
-
Save twillis/7604032 to your computer and use it in GitHub Desktop.
for this linked in discussion http://www.linkedin.com/groupItem?view=&gid=1846027&type=member&item=5809095960126590978&qid=c6560208-ab58-45fb-9046-f8485124f631&trk=groups_most_popular-0-b-ttl&goback=%2Egmr_1846027%2Egde_1846027_member_5809095960126590978%2Egmp_1846027
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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