What is the Python equivalent of Matlab's tic and toc functions?
Apart from timeit
which ThiefMaster mentioned, a simple way to do it is just (after importing time
):
t = time.time()# do stuffelapsed = time.time() - t
I have a helper class I like to use:
class Timer(object): def __init__(self, name=None): self.name = name def __enter__(self): self.tstart = time.time() def __exit__(self, type, value, traceback): if self.name: print('[%s]' % self.name,) print('Elapsed: %s' % (time.time() - self.tstart))
It can be used as a context manager:
with Timer('foo_stuff'): # do some foo # do some stuff
Sometimes I find this technique more convenient than timeit
- it all depends on what you want to measure.
I had the same question when I migrated to python from Matlab. With the help of this thread I was able to construct an exact analog of the Matlab tic()
and toc()
functions. Simply insert the following code at the top of your script.
import timedef TicTocGenerator(): # Generator that returns time differences ti = 0 # initial time tf = time.time() # final time while True: ti = tf tf = time.time() yield tf-ti # returns the time differenceTicToc = TicTocGenerator() # create an instance of the TicTocGen generator# This will be the main function through which we define both tic() and toc()def toc(tempBool=True): # Prints the time difference yielded by generator instance TicToc tempTimeInterval = next(TicToc) if tempBool: print( "Elapsed time: %f seconds.\n" %tempTimeInterval )def tic(): # Records a time in TicToc, marks the beginning of a time interval toc(False)
That's it! Now we are ready to fully use tic()
and toc()
just as in Matlab. For example
tic()time.sleep(5)toc() # returns "Elapsed time: 5.00 seconds."
Actually, this is more versatile than the built-in Matlab functions. Here, you could create another instance of the TicTocGenerator
to keep track of multiple operations, or just to time things differently. For instance, while timing a script, we can now time each piece of the script seperately, as well as the entire script. (I will provide a concrete example)
TicToc2 = TicTocGenerator() # create another instance of the TicTocGen generatordef toc2(tempBool=True): # Prints the time difference yielded by generator instance TicToc2 tempTimeInterval = next(TicToc2) if tempBool: print( "Elapsed time 2: %f seconds.\n" %tempTimeInterval )def tic2(): # Records a time in TicToc2, marks the beginning of a time interval toc2(False)
Now you should be able to time two separate things: In the following example, we time the total script and parts of a script separately.
tic()time.sleep(5)tic2()time.sleep(3)toc2() # returns "Elapsed time 2: 5.00 seconds."toc() # returns "Elapsed time: 8.00 seconds."
Actually, you do not even need to use tic()
each time. If you have a series of commands that you want to time, then you can write
tic()time.sleep(1)toc() # returns "Elapsed time: 1.00 seconds."time.sleep(2)toc() # returns "Elapsed time: 2.00 seconds."time.sleep(3)toc() # returns "Elapsed time: 3.00 seconds."# and so on...
I hope that this is helpful.
The absolute best analog of tic and toc would be to simply define them in python.
def tic(): #Homemade version of matlab tic and toc functions import time global startTime_for_tictoc startTime_for_tictoc = time.time()def toc(): import time if 'startTime_for_tictoc' in globals(): print "Elapsed time is " + str(time.time() - startTime_for_tictoc) + " seconds." else: print "Toc: start time not set"
Then you can use them as:
tic()# do stufftoc()