[PYTHON] Write processing time measurement a little easier using the with clause

Common processing time measurement

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?

What's wrong

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.

Let's write using the with clause

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

Write processing time measurement a little easier using the with clause
Time measurement using a clock
Write a TCP server using the SocketServer module
How to write a GUI using the maya command
Let's make dependency management with pip a little easier
A little bit from Python using the Jenkins API
Make a Linux version of OpenSiv3D with find_package a little easier
[Linux] Write a deployment tool using rsync with a shell script
Calculation time measurement using maf
Using a printer with Debian 10
Execution time measurement with Python With
A little stuck with chainer
Control the motor with a motor driver using python on Raspberry Pi 3!
On Linux, the time stamp of a file is a little past.
A function that measures the processing time of a method in python
Write a script to calculate the distance with Elasticsearch 5 system painless
Write a Residual Network with TFLearn
Make C compilation a little easier
Using cgo with the go command
Write a stacked histogram with matplotlib
Using a webcam with Raspberry Pi
Write a batch script with Python3.5 ~
python> Processing time measurement> time.time () --start_time
Think about how to write a filter with the Shotgun API-Contact Versions
The story of having a hard time introducing OpenCV with M1 MAC
How to fix the initial population with a genetic algorithm using DEAP
Receive a list of the results of parallel processing in Python with starmap
[Introduction to Python] How to write a character string with the format function
Create a record with attachments in KINTONE using the Python requests module
I made a tool that makes decompression a little easier with CLI (Python3)
A useful note when using Python for the first time in a while
Python: I want to measure the processing time of a function neatly
Create a command to search for similar compounds from the target database with RDKit and check the processing time