A clever way to time processing in Python

version

Python uses 3.6.0. At stop_watch.py

print(f"{func.__name__}Is{elapsed_time}It took a second")

However, this notation does not work unless it is 3.6.0 or higher. Please rewrite as appropriate according to your version. The other parts are unconfirmed, but I think it will work with 3x.

Related article

To measure time with Python, basically use the method in the following article.

[Python] Measure and display the time required for processing python> Processing time measurement> time.time () --start_time Function that measures the processing time of a method with python

You can also measure the time with the above method. However, the logic you want to write and the time measurement are combined.

Therefore, there is a clever way to completely separate logic and time measurement, so I will share it.

Processing to measure time

stop_watch.py


from functools import wraps
import time
def stop_watch(func) :
    @wraps(func)
    def wrapper(*args, **kargs) :
        start = time.time()
        result = func(*args,**kargs)
        elapsed_time =  time.time() - start
        print(f"{func.__name__}Is{elapsed_time}It took a second")
        return result
    return wrapper

The above code is the process to measure the time. This code uses a decorator. The actual processing is stored in func, the measurement start time is stored in start, and the time taken for processing is stored in ʻelaspsed_time`. If you would like to know more about decorators, please read this article in detail. 12 Steps to Understanding Python Decorators

Actual usage

test.py


from stop_watch import stop_watch

@stop_watch
def func() :
    j=0
    for i in range(99999999) :
        j+=i
    print(j)

func()

The actual usage is like this. Just write @stop_watch above the function you want to measure the time. (* It is assumed that the above stop_watch.py and the following test.py are in the same directory (folder). If they are different, please rewrite the import process as appropriate)

Result is,

result


4999999850000001
func is 6.330682277679443 It took 2 seconds

Is displayed. The result of the function whose time you want to measure is displayed on the first line. On the second line, the name of the function for which you want to measure the time and the time taken to execute that function are displayed.

Miscellaneous feelings

It has nothing to do with logic, but if you want to see some results, the decorator will shine. This time, it was a task of measuring time. I hope it helps something.

Recommended Posts

A clever way to time processing in Python
How to measure processing time in Python or Java
A simple way to avoid multiple for loops in Python
A standard way to develop and distribute packages in Python
Introducing a good way to manage DB connections in Python
How to get a stacktrace in python
Easy way to use Wikipedia in Python
A way to understand Python duck typing
A function that measures the processing time of a method in python
An easy way to view the time taken in Python and a smarter way to improve it
Try to calculate a statistical problem in Python
File processing in Python
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
Multithreaded processing in python
Type Python scripts to run in QGIS Processing
I want to create a window in Python
How to create a JSON file in Python
Text processing in Python
Steps to develop a web application in Python
Queue processing in Python
To add a module to python put in Julialang
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
Python: I want to measure the processing time of a function neatly
What is the fastest way to create a reverse dictionary in python?
To represent date, time, time, and seconds in Python
How to stop a program in python until a specific date and time
Searching for an efficient way to write a Dockerfile in Python with poetry
Convert timezoned date and time to Unixtime in Python2.7
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
[Python] How to expand variables in a character string
I tried to implement a pseudo pachislot in Python
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
I want to work with a robot in python.
Things to note when initializing a list in Python
Introduction to Linear Algebra in Python: A = LU Decomposition
[Python] Created a method to convert radix in 1 second
Multi-digit multiplication time up to 300 million digits in python
How to execute a command using subprocess in Python
Things to keep in mind when processing strings in Python2
Fixed a way to force Windows to boot in UEFI
Publish / upload a library created in Python to PyPI
Things to keep in mind when processing strings in Python3
Take a screenshot in Python
UTF8 text processing in python
Create a function in Python
Create a dictionary in Python
A road to intermediate Python
Login to website in Python
Asynchronous processing (threading) in python
Make a bookmarklet in Python
Speech to speech in python [text to speech]
[Python] Another way to import
Image Processing Collection in Python