Skip to content

Instantly share code, notes, and snippets.

@ajp619
Created October 19, 2013 16:17
Show Gist options
  • Save ajp619/7057983 to your computer and use it in GitHub Desktop.
Save ajp619/7057983 to your computer and use it in GitHub Desktop.
Quick python timer example
import timeit
def func(x, y):
return x**2 + y**2
def wrapper(func, *args, **kwargs):
"""This enables using a function with arguments with timeit"""
def wrapped():
return func(*args, **kwargs)
return wrapped
def timer(func, *args, **kwargs):
r = 3
num = 1000
wrapped = wrapper(func, *args, **kwargs)
t = timeit.Timer(wrapped)
return min(t.repeat(r, number=num))/num
def main():
print timer(func, 3, 4)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment