Created
October 19, 2013 16:17
-
-
Save ajp619/7057983 to your computer and use it in GitHub Desktop.
Quick python timer example
This file contains hidden or 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
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