I spend a lot of time studying different aspects of Python, different implementation techniques, and so forth. As part of that, I often carry out little performance benchmarks. For small things, I'll often use the timeit module. For example:
>>> from timeit import timeit >>> timeit("math.sin(2)","import math") 0.29826998710632324 >>> timeit("sin(2)","from math import sin") 0.21983098983764648 >>>
However, for larger blocks of code, I tend to just use the time module directly like this:
import time start = time.time() ... ... some big calculation ... end = time.time() print("Whatever : %0.3f seconds" % (end-start))
Having typed that particular code template more often than I care to admit, it occurred to me that I really ought to just make a context manager for doing it. Something like this:
# benchmark.py import time class benchmark(object): def __init__(self,name): self.name = name def __enter__(self): self.start = time.time() def __exit__(self,ty,val,tb): end = time.time() print("%s : %0.3f seconds" % (self.name, end-self.start)) return False
Now, I can just use that context manager whenever I want to do that kind of timing benchmark. For example:
# fileitertest.py from benchmark import benchmark with benchmark("iterate by lines (UTF-8)"): for line in open("biglog.txt",encoding='utf-8'): pass with benchmark("iterate by lines (Latin-1)"): for line in open("biglog.txt",encoding='latin-1'): pass with benchmark("iterate by lines (Binary)"): for line in open("biglog.txt","rb"): pass
If you run it, you might get output like this:
bash % python3 fileitertest.py iterate by lines (UTF-8) : 3.903 seconds iterate by lines (Latin-1) : 3.615 seconds iterate by lines (Binary) : 1.886 seconds
Nice. I like it already!
08/01/2009 - 09/01/2009 09/01/2009 - 10/01/2009 10/01/2009 - 11/01/2009 11/01/2009 - 12/01/2009 12/01/2009 - 01/01/2010 01/01/2010 - 02/01/2010 02/01/2010 - 03/01/2010 04/01/2010 - 05/01/2010 05/01/2010 - 06/01/2010 07/01/2010 - 08/01/2010 08/01/2010 - 09/01/2010 09/01/2010 - 10/01/2010 12/01/2010 - 01/01/2011 01/01/2011 - 02/01/2011 02/01/2011 - 03/01/2011 03/01/2011 - 04/01/2011 04/01/2011 - 05/01/2011 05/01/2011 - 06/01/2011 08/01/2011 - 09/01/2011 09/01/2011 - 10/01/2011 12/01/2011 - 01/01/2012 01/01/2012 - 02/01/2012 02/01/2012 - 03/01/2012 03/01/2012 - 04/01/2012 07/01/2012 - 08/01/2012 01/01/2013 - 02/01/2013 03/01/2013 - 04/01/2013 06/01/2014 - 07/01/2014 09/01/2014 - 10/01/2014
Subscribe to Posts [Atom]