Skip to content
Karl edited this page May 2, 2015 · 1 revision

Pythonbenchmark

The Python library that makes benchmarking easy and fun

Contributors wanted. Please send me lots of pull requests or suggestions
The timeit module that comes with python is only useful for small bits of Python code not for functions.
This library solves that. It provides an intuitive way to measure the execution time of functions and compare the relative speed of two functions.

Why

would you want to compare the relative speed of two functions? Because if you are optimizing your code you may want to know how much of a speed improvement you gained.

How

Pythonbenchmark allows this by letting you compare two functions which take the same input and measure which one gets the job done faster.

@measure

Additionally you can just put a decorator on the functions, pythonbenchmark will measure them and print out the execution time in the console.

Howto

: The typical use case could be: You have functionX, and optimized functionX. Now you want to know if your modified version is faster.
from pythonbenchmark import compare, measure
import time

a,b,c,d,e = 10,10,10,10,10
something = [a,b,c,d,e]

def myFunction(something):
	time.sleep(0.4)

def myOptimizedFunction(something):
	time.sleep(0.2)

# comparing test
compare(myFunction, myOptimizedFunction, 10, input)

Output

![alt tag](https://github.com/Karlheinzniebuhr/pythonbenchmark/blob/master/test_files/test_compare.PNG)

Measuring execution time with the @measure decorator

from pythonbenchmark import compare, measure
import time

a,b,c,d,e = 10,10,10,10,10
something = [a,b,c,d,e]

@measure
def myFunction(something):
	time.sleep(0.4)

@measure
def myOptimizedFunction(something):
	time.sleep(0.2)

myFunction(input)
myOptimizedFunction(input)

Output

![alt tag](https://github.com/Karlheinzniebuhr/pythonbenchmark/blob/master/test_files/test_decorator.PNG)

TODO

Adding support to compare multiple functions maybe? Any ideas are welcome