[PYTHON] Easily cProfile with a decorator

If you write a library of a reasonable size, it will be troublesome to execute a script from the command line and write setUp and tearDown of unittest every time you profile, so make a decorator.

d_profile.py



import cProfile
import pstats


def profile(func):
    def _f(*args, **kwargs):
        pr = cProfile.Profile()
        pr.enable()
        print("\n<<<---")
        res = func(*args, **kwargs)
        p = pstats.Stats(pr)
        p.strip_dirs().sort_stats('cumtime').print_stats(20)
        print("\n--->>>")
        return res
    return _f

You can pinpoint profile only for specific functions and methods. Comment out the decorator if you want to disable the profile. Of course, you can also use it when you want to profile only a specific test method with unittest.

func.py



from d_profile import profile

@profile
def hoge(a, b):
    return a * b

Recommended Posts

Easily cProfile with a decorator
Decorate with a decorator
Easily draw a map with matplotlib.basemap
Create a Python function decorator with Class
Easily build a development environment with Laragon
You can easily create a GUI with Python
Create a Python console application easily with Click
Easily daemonized with Supervisor
A4 size with python-pptx
Make a function decorator
Easily beep with python
Why not create a stylish table easily with Python?
Learn librosa with a tutorial 1
Easily serverless with Python with chalice
Draw a graph with NetworkX
Try programming with a shell!
Easily write if-elif with lambda
Create a homepage with django
Using a printer with Debian 10
Make a fortune with Python
Creating a GUI as easily as possible with python [tkinter edition]
Tornado-Let's create a Web API that easily returns JSON with JSON
Create a directory with python
A little stuck with chainer
Draw a graph with networkx
Make a fire with kdeplot
Easily build a natural language processing model with BERT + LightGBM + optuna
Building a kubernetes environment with ansible 2
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
A python graphing manual with Matplotlib.
Play with a turtle with turtle graphics (Part 1)
Draw a graph with Julia + PyQtGraph (2)
Generate a normal distribution with SciPy
Creating a decision tree with scikit-learn
Let's make a GUI with python.
Make a sound with Jupyter notebook
Enumerate files with a specific extension
Creating a Flask server with Docker
Build a deb file with Docker
Easily implement subcommands with python click
Easily handle lists with python + sqlite3
Draw a loose graph with matplotlib
Deploy a Django application with Docker
Let's make a breakout with wxPython
Create a virtual environment with Python!
I made a fortune with Python.
Draw a beautiful circle with numpy
Easily switch pyenv virtualenvs with peco
Write a Residual Network with TFLearn
Django Tips-Create a ranking site with Django-
Draw a graph with Julia + PyQtGraph (1)
Building a virtual environment with Python 3
Creating a simple app with flask
Draw a graph with Julia + PyQtGraph (3)
Solve ABC168 A ~ C with Python
Realize a queue with two stacks
Make a recommender system with python
Easily handle databases with Python (SQLite3)
Build a web application with Django
Generate a Pre-Signed URL with golang