PK ¸R‘C½…E E " chrono-bench-latest/searchindex.jsSearch.setIndex({objects:{},terms:{all:[1,2],code:[0,2],help:2,execut:0,less:2,via:2,follow:2,still:2,bunch:2,impact:2,onli:2,pretti:2,activ:1,should:2,fib:2,els:2,might:2,alter:2,bin:1,good:2,"return":2,around:2,get:[0,2],python:[0,2],timestamp:2,veri:2,now:2,python3:1,like:2,env3:1,elaps:2,easili:0,nose:1,small:2,output:2,where:2,manag:[0,2],kernel:2,right:2,some:2,back:2,maintain:1,second:2,backport:2,pass:[1,2],http:1,librari:2,virtualenv:1,index:0,timeit:2,cleanli:0,someth:2,below:2,content:0,written:2,larg:2,print:2,"import":2,approach:2,refer:2,toastdriven:1,perfect:2,object:2,run:[0,1,2],let:2,valu:2,shine:2,search:0,easi:2,could:2,timer:[0,2],page:0,thing:2,tutori:[0,2],nosetest:1,context:[0,2],improv:2,com:1,licens:0,rang:2,"__future__":2,effici:2,point:2,schedul:2,modul:[0,2],"float":2,number:2,unix:2,wrap:2,instal:[0,1],your:2,leverag:2,better:2,git:1,from:2,would:2,support:2,wrapper:2,start:[0,2],much:2,call:2,low:2,lot:2,taken:2,store:2,"function":2,statement:2,benchmark:[0,2],github:1,than:2,look:2,instanc:2,provid:2,setup:[1,2],work:2,calcul:2,sampl:2,similar:2,def:2,sai:2,want:2,py3:2,everydai:0,indic:0,pip:[0,1],simpli:2,fibonacci:2,have:2,tabl:0,need:2,unfortun:2,differenti:2,end:2,loop:[0,2],rather:2,make:2,format:2,when:2,write:2,also:2,wai:2,test:[0,1],you:2,properti:2,simpl:2,complet:2,sure:2,unless:2,our:2,clone:1,after:2,invok:2,bsd:0,averag:2,coupl:2,chrono:[0,1,2],later:2,"switch":2,exampl:2,thi:2,time:[0,1,2],everyth:2,cpu:2,awesom:2},objtypes:{},titles:["chrono","Running Tests","chrono Tutorial"],objnames:{},filenames:["index","testing","tutorial"]})PK ¸R‘CŸÎ˜zm m chrono-bench-latest/testing.html
Setup:
$ git clone https://github.com/toastdriven/chrono.git
$ cd chrono
$ virtualenv -p python3 env3
$ . env3/bin/activate
$ pip install nose
Running:
$ nosetests -s -v tests.py
chrono is maintained with 100% passing tests at all times.
chrono is a pretty small & simple library. It leverages the context manager support added in Python 2.6+ (backported to Python 2.5 via __future__). It also works on Python 3.3+ (all examples below are written using Py3).
It provides a simple context manager that you wrap around the code you want to time.
For example, let’s say you want to time your awesome Fibonacci function & you want to make sure it’s efficient. Your code might look something like...:
def fib(n):
if n in (0, 1):
return n
else:
return fib(n - 1) + fib(n - 2)
To time it using chrono, you’d call it in the following way...:
from chrono import Timer
with Timer() as timed:
print(fib(10))
print("Seconds taken: {0}".format(timed.elapsed))
If everything’s setup right, you should get something like the following as output:
55
Seconds taken: 0.000102996826172
Unfortunately, unless you pass a very large number to fib, the time taken is going to be very low & you’re not going to get a good sample. If only we could run that function with small values but do it a bunch of times...
This is where the context manager approach shines. Rather than having to write a wrapper function or similar (like you have to do with the timeit module), you simply need to alter your benchmarking code to use a loop...:
from chrono import Timer
with Timer() as timed:
for i in range(100):
print(fib(10))
print("Seconds taken: {0}".format(timed.elapsed))
Now I get this as output:
# Lots of "55"s, then...
Seconds taken: 0.0073549747467
Much better. Now things like CPU context switches & kernel scheduling will have less of an impact on our benchmark. It’s still not perfect (things like averages would help some), but it’s an improvement & it was easy.
When invoked with the with Timer() as ...: statement, you get back the Timer instance. It’s a simple object with a couple of useful properties.