Execution time measurement with Python With

To be able to

You will be able to measure time using with. It can be used when measuring time with Kaggle etc.

reference

Used in Mercari Competition 1st Code ..

how to use

Just write @contextmanager just before defining the function you want to use as the context manager.

When timer is called, the initial time is stored in t0, and yeild exits the temporary function. After the processing of time.sleep (1) is completed, it returns to timer again and outputs the difference from the initial time.

qiita.rb


from contextlib import contextmanager
import time

@contextmanager
def timer(name):
    t0 = time.time()
    print("start")
    yield
    print("end")
    print(f'[{name}] done in {time.time() - t0:.0f} s')


with timer('process train'): 
        time.sleep(1)

image.png

If @contextmanager is not described, timer cannot operate as contextmanager and an error will occur. image.png

Context manager

[with minutes and context manager] (https://docs.python.org/ja/3/reference/datamodel.html#context-managers "Qiita") The context manager handles the entry and exit processing required to execute a block of code.

As shown in the following example, the context manager class describes the process for starting and the process for ending. Therefore, if you make it a context manager with @contextmanager, timer will be called at the start and end. If @contextmanager is not described, an error occurs because enter is not defined. image.png Reference

Recommended Posts

Execution time measurement with Python With
Python execution time measurement memo
Function execution time (Python)
Measurement of execution time
How to measure execution time with Python Part 1
How to measure execution time with Python Part 2
Time synchronization (Windows) with Python
Measure function execution time in Python
Python (from first time to execution)
Easy parallel execution with python subprocess
python> Processing time measurement> time.time () --start_time
FizzBuzz with Python3
First time python
Scraping with Python
measurement of time
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
First time python
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Try to make foldl and foldr with Python: lambda. Also time measurement
Prepare the execution environment of Python3 with Docker
[Python3] A story stuck with time zone conversion
Build PyPy and Python execution environment with Docker
Build a python execution environment with VS Code
✨ Easy with Python ☆ Estimated elapsed time after death ✨
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Python: Time Series Analysis
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python