import time
start = time.time() #Start measurement
do_something() #Execute the processing of the measurement target
end = time.time() #End of measurement
print 'CalcTime: {}'.format(end-start)
Do you have your own preference for the module to use, time or datetime?
It's not bad. However, ** ・ Forget to delete start and end and commit ** ** ・ I don't know what I wrote for measurement because I embedded too many things ** ** ・ It is troublesome to write start and end in the first place ** There are various things.
with clause
The feature is that `__enter__``` at the start and
`__ exit__``` at the end are called respectively.
It is also often used for processing such as file IO.
It seems that it can be used for measurement processing when it is executed at the start and end.
When I actually write it,
import time
#Class to measure processing time
class ProcessingTimer(object):
def __init__(self):
self.start_time = 0 #Start time
def __enter__(self):
self.start_time = time.time()
def __exit__(self, exc_type, exc_val, exc_tb):
print 'processing time: {}'.format(time.time() - self.start_time)
def func()
with ProcessingTimer():
do_something()
When executed, it looks like this (the result is appropriate)
>>>> func()
processing time: 0.0352
I wrote it with the minimum configuration, but it seems that various things can be done by customizing the Processing Timer. If you make it like this, when you are a little worried about the processing time ** 1. With ProcessingTimer (): Add ** ** 2. Collectively indent the part you want to measure ** You will be able to measure with. When removing ** 1. Erase with ProcessingTimer (): ** ** 2. Collectively indent the measured part ** Is OK. (I feel like it's convenient for me)
Recommended Posts