Skip to content

Instantly share code, notes, and snippets.

@mgius
Created November 20, 2012 20:27
Show Gist options
  • Save mgius/4120839 to your computer and use it in GitHub Desktop.
Save mgius/4120839 to your computer and use it in GitHub Desktop.
mapsvslist.py
withMap count 1000 duration 0.498514890671
withList count 1000 duration 0.469129085541
withListAppend count 1000 duration 0.613918066025
withMap count 2000 duration 1.12835097313
withList count 2000 duration 0.877814054489
withListAppend count 2000 duration 1.24980807304
withMap count 3000 duration 1.59243202209
withList count 3000 duration 1.32240200043
withListAppend count 3000 duration 1.87615203857
withMap count 4000 duration 2.06562519073
withList count 4000 duration 1.69773697853
withListAppend count 4000 duration 2.46374106407
withMap count 5000 duration 2.52711892128
withList count 5000 duration 2.16213989258
withListAppend count 5000 duration 3.0658390522
withMap count 6000 duration 3.65477108955
withList count 6000 duration 2.51911497116
withListAppend count 6000 duration 3.63686203957
withMap count 7000 duration 4.10053992271
withList count 7000 duration 2.96284890175
withListAppend count 7000 duration 4.26587700844
withMap count 8000 duration 4.54991984367
withList count 8000 duration 3.3590631485
withListAppend count 8000 duration 4.86335492134
withMap count 9000 duration 4.99911093712
withList count 9000 duration 3.75384688377
withListAppend count 9000 duration 5.47402691841
import timeit
def withMap(count=1000):
data = {}
for val in xrange(count):
data[val] = val
def withList(count=1000):
data = [None] * count
for val in xrange(count):
data[val] = val
def withListAppend(count=1000):
data = []
for val in xrange(count):
data.append(val)
def main():
for x in range(1000, 10000, 1000):
mapLambda = lambda: withMap(x)
listLambda = lambda: withList(x)
listAppendLambda = lambda: withListAppend(x)
duration = timeit.timeit(mapLambda, number=10000)
print "withMap count %d duration %s" % (x, duration)
duration = timeit.timeit(listLambda, number=10000)
print "withList count %d duration %s" % (x, duration)
duration = timeit.timeit(listAppendLambda, number=10000)
print "withListAppend count %d duration %s" % (x, duration)
print ""
if __name__ == '__main__':
main()
withMap count 1000 duration 0.239351987839
withList count 1000 duration 0.0933339595795
withListAppend count 1000 duration 0.160182952881
withMap count 2000 duration 0.722405910492
withList count 2000 duration 0.16973400116
withListAppend count 2000 duration 0.288941144943
withMap count 3000 duration 0.88702082634
withList count 3000 duration 0.266885042191
withListAppend count 3000 duration 0.46639084816
withMap count 4000 duration 1.05926513672
withList count 4000 duration 0.362340927124
withListAppend count 4000 duration 0.609817028046
withMap count 5000 duration 1.22679400444
withList count 5000 duration 0.453685045242
withListAppend count 5000 duration 0.783066987991
withMap count 6000 duration 2.49516510963
withList count 6000 duration 0.552264928818
withListAppend count 6000 duration 0.894737005234
withMap count 7000 duration 2.67384886742
withList count 7000 duration 0.648275136948
withListAppend count 7000 duration 1.13814496994
withMap count 8000 duration 2.76917600632
withList count 8000 duration 0.765680074692
withListAppend count 8000 duration 1.28613495827
withMap count 9000 duration 2.94410800934
withList count 9000 duration 0.861002922058
withListAppend count 9000 duration 1.46240401268
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment