Created
November 6, 2012 16:36
-
-
Save njsmith/4025870 to your computer and use it in GitHub Desktop.
quick hack for timing import speed
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
# 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