Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Last active August 29, 2015 14:01
Show Gist options
  • Save bradmontgomery/17a1f49ae5e29574b5a8 to your computer and use it in GitHub Desktop.
Save bradmontgomery/17a1f49ae5e29574b5a8 to your computer and use it in GitHub Desktop.
Timing some things in python 2 and python 3. You should use a list comprehension instead of map + lambda, and/or a list comprehension with sum instead of reduce + lambda (python 2), but that's no necessarily the case in python 3!
#!/usr/bin/env python
# Run this in python 2
from timeit import timeit
# Use a list comprehension instead of map & lambda
# ------------------------------------------------
code = "map(lambda x: {'id': x}, range(100))"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 21.14860200881958
code = "[{'id': x} for x in range(100)]"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 14.261168003082275
# Use a list comprehension instead of using filter
# map, and lambda to split and clean strings.
# ------------------------------------------------
code = "filter(None, map(lambda x: x.strip(), 'foo, bar, , ,'.split(',')))"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 1.441457986831665
code = "[s.strip() for s in 'foo, bar, , ,'.split(',')]"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 0.7249608039855957
# Use sum and a list comprehension instead of reduce & lambda
# -----------------------------------------------------------
code = "reduce(lambda x,y: x + y, range(100), 0)"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 14.536792993545532
code = "sum(range(100))"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 2.007258892059326
# Should I call dict.update or access by the key?
# -----------------------------------------------
setup = "d = {}"
code = "d.update({'foo': 'bar'})"
n = 10000000 # this needs a few more iterations to make a difference
print(code)
print("--> {0}\n".format(timeit(code, setup=setup, number=n))) # ~ 4.08743095398
code = "d['foo'] = 'bar'"
print(code)
print("--> {0}\n".format(timeit(code, setup=setup, number=n))) # ~ 0.677881002426
# Python 3!
# ---------
from timeit import timeit
# map + lambda is MUCH faster than in python 2!
# ---------------------------------------------
code = "map(lambda x: {'id': x}, range(100))"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 0.7829951889943914
code = "[{'id': x} for x in range(100)]"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 35.88625135499751
# filter, map, and lambda are faster in python 3!
# -----------------------------------------------
code = "filter(None, map(lambda x: x.strip(), 'foo, bar, , ,'.split(',')))"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 1.2543795250021503
code = "[s.strip() for s in 'foo, bar, , ,'.split(',')]"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 2.219071236999298
# Use sum and a list comprehension instead of reduce & lambda
# (This still holds over from python 2)
# -----------------------------------------------------------
code = "reduce(lambda x,y: x + y, range(100), 0)"
print(code)
print("--> {0}\n".format(timeit(code, setup="from functools import reduce"))) # ~ 18.44832423700427
code = "sum(range(100))"
print(code)
print("--> {0}\n".format(timeit(code))) # ~ 2.348026789004507
# Should I call dict.update or access by the key?
# -----------------------------------------------
setup = "d = {}"
code = "d.update({'foo': 'bar'})"
n = 10000000 # this needs a few more iterations to make a difference
print(code)
print("--> {0}\n".format(timeit(code, setup=setup, number=n))) # ~ 4.279781358993205
code = "d['foo'] = 'bar'"
print(code)
print("--> {0}\n".format(timeit(code, setup=setup, number=n))) # ~ 0.6867842049978208
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment