How to measure execution time with Python Part 2

How to measure execution time with Python Part 2

The decorator used in Decorator for measuring execution time # 1 returned the execution time. To display the execution time while returning the original value of the function to decorate: Although it is output to the standard output by print, I think that it is also good to use a logging module etc.

Redefining the time decorator

def time(func):
    """
Debug decorator
Decorator to display effective time
    """
    import functools
    import datetime
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = datetime.datetime.today()
        print '-------------------------------------------------------------'
        print func
        print '----------------------------------'
        print 'start:', start
        result = func(*args, **kwargs)
        end = datetime.datetime.today()
        print 'end:', end
        print '----------------------------------'
        print 'running:', end - start
        print '-------------------------------------------------------------'
        return result
    return wrapper

run the time decorator

I tried using a new decorator for testfunc1 used in Decorator for measuring execution time 1.

>>> testfunc1(rangelist)
-------------------------------------------------------------
<function testfunc1 at 0x01c40590>
----------------------------------
start: 2014-03-21 23:21:01.947000
end: 2014-03-21 23:21:02.362000
----------------------------------
running: 0:00:00.415000
-------------------------------------------------------------

The execution time is displayed properly.

Decorator that does nothing

If you don't want to debug and it's a hassle to rewrite the function definition one by one, you can overwrite the time decorator with a do-nothing decorator. As you can see by using it, IPython etc. cannot complete the argument by tab. Since we are using functools.wapas, if you write the docstring properly, the docstring can be displayed even with a function wrapped in a decorator.

def time(func):
    """
Decorator that does nothing
    """
    import functools
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

Recommended Posts

How to measure execution time with Python Part 1
How to measure execution time with Python Part 2
How to measure mp3 file playback time with python
Execution time measurement with Python With
How to measure processing time in Python or Java
How to upload with Heroku, Flask, Python, Git (Part 3)
How to upload with Heroku, Flask, Python, Git (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 2)
Python: How to use async with
Measure function execution time in Python
How to get started with Python
Python (from first time to execution)
How to calculate date with python
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to work with BigQuery in Python
How to do portmanteau test with python
[Part1] Scraping with Python → Organize to csv!
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to install python3 with docker centos
[Python3] Define a decorator to measure the execution time of a function
How to write offline real time Solve F01 problems with Python
How to utilize Python with Jw_cad (Part 1 What is external transformation)
How to build Python and Jupyter execution environment with VS Code
How to calculate "xx time" in one shot with Python timedelta
How to upload with Heroku, Flask, Python, Git (4)
How to read a CSV file with Python 2/3
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
How to install Python
Function execution time (Python)
Strategy on how to monetize with Python Java
How to achieve time wait processing with wxpython
How to crop the lower right part of the image with Python OpenCV
How to write offline real time I tried to solve E11 with python
How to use Python Kivy ④ ~ Execution on Android ~
Quit Python execution with Ctrl-C (responds to SIGINT)
[Python] How to draw multiple graphs with Matplotlib
[Python] How to read excel file with pandas
How to crop an image with Python + OpenCV
How to install python
How to specify attributes with Mock of python
Output python execution time
How to use tkinter with python in pyenv
[Python] How to handle Japanese characters with openCV
[Python] How to compare datetime with timezone added
How to write offline real time I tried to solve E12 with python
How to add help to HDA (with Python script bonus)
[Python] How to draw a line graph with Matplotlib
How to do hash calculation with salt in Python
[Introduction to Python] How to iterate with the range function?
Explain in detail how to make sounds with python
How to run tests in bulk with Python unittest
[Python] How to specify the download location with youtube-dl
How to make a shooting game with toio (Part 1)
How to use python interactive mode with git bash
To avoid spending time coloring shapes drawn with python
How to convert JSON file to CSV file with Python Pandas
[Python] How to deal with pandas read_html read error
How to get mouse wheel verdict with Python curses