Created
September 28, 2014 09:05
-
-
Save alexland/96a814a77881e8bd8f81 to your computer and use it in GitHub Desktop.
lightweight class for timing python code
This file contains 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
#!/usr/local/bin/python2.7 | |
# encoding: utf-8 | |
import time | |
from timeit import default_timer | |
class Timer(object): | |
def __init__(self, verbose=True): | |
self.verbose = verbose | |
self.timer = default_timer | |
def __enter__(self): | |
self.start = self.timer() | |
return self | |
def __exit__(self, *args): | |
end = self.timer() | |
self.elapsed_secs = end - self.start | |
self.elapsed = self.elapsed_secs * 1000 | |
if self.verbose: | |
print("elapsed time: {0:3f} milliseconds".format(self.elapsed)) | |
if __name__ == '__main__': | |
import numpy as NP | |
from scipy import linalg as LA | |
with Timer() as t: | |
# code block that you wish to measure execution speed | |
A = NP.random.randn(100*100).reshape(100, 100) | |
eve, eva = LA.eig(A) | |
"elapsed time: {0:2f} milliseconds".format(t.elapsed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment