Skip to content

Instantly share code, notes, and snippets.

@njsmith
Created November 6, 2012 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save njsmith/4025870 to your computer and use it in GitHub Desktop.
Save njsmith/4025870 to your computer and use it in GitHub Desktop.
quick hack for timing import speed
# To measure cold-cache import speed:
# $ echo 3 > /proc/sys/vm/drop_caches
# >>> timeimport("mymod", 1)
#
# To measure hot-cache import speed:
# >>> timeimport("mymod", 1) # warm the cache
# >>> timeimport("mymod", 10) # make the measurement
import sys
import timeit
def timeimport(modname, N=50):
orig_mods = set(sys.modules)
def cleanimport():
__import__(modname)
for mod in list(sys.modules):
if mod not in orig_mods:
del sys.modules[mod]
return timeit.timeit(cleanimport, number=N) * 1. / N
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment