Skip to main content

A timer for profiling a Python function or snippet.

Project description


A timer for profiling a Python function or snippet.


  • Filtering external libraries profiling.
  • Filtering very short time functions-> threshold
  • Allow depth: you can easily find the time consuming function
  • Allow loop or multiple function call
  • Allow recursive function call
  • Disable it globaly by Profiler.GlobalDisable=True to save time :)


Release version:

$ pip install auto_profiler

Development version:

$ pip install -e git+

Quick start

Auto profiling

More commonly, chances are that we want to measure the execution time of an entry function and all its subfunctions. In this case, it's too tedious to do it manually, and we can leverage Profiler to inject all the timing points for us automatically:

import time # line number 1
import random

from auto_profiler import Profiler, Tree

def f1():

def mysleep(t):

def fact(i):
        return 1
    return i*fact(i-1)

def show(p):
    print('Time   [Hits * PerHit] Function name [Called from] [Function Location]\n'+\
    print(Tree(p.root, threshold=0.5))

@Profiler(depth=4, on_disable=show)
def main():
    for i in range(5):


if __name__ == '__main__':

Example Output

Time   [Hits * PerHit] Function name [Called from] [function location]
8.974s [1 * 8.974]  main  [auto-profiler/]  [/test/]
├── 5.954s [5 * 1.191]  f1  [/test/]  [/test/]
│   └── 5.954s [5 * 1.191]  mysleep  [/test/]  [/test/]
│       └── 5.954s [5 * 1.191]  <time.sleep>
|   # The rest is for the example recursive function call fact
└── 3.020s [1 * 3.020]  fact  [/test/]  [/test/]
    ├── 0.849s [1 * 0.849]  f1  [/test/]  [/test/]
    │   └── 0.849s [1 * 0.849]  mysleep  [/test/]  [/test/]
    │       └── 0.849s [1 * 0.849]  <time.sleep>
    └── 2.171s [1 * 2.171]  fact  [/test/]  [/test/]
        ├── 1.552s [1 * 1.552]  f1  [/test/]  [/test/]
        │   └── 1.552s [1 * 1.552]  mysleep  [/test/]  [/test/]
        └── 0.619s [1 * 0.619]  fact  [/test/]  [/test/]
            └── 0.619s [1 * 0.619]  f1  [/test/]  [/test/]

Manual profiling

Sometimes, we only want to measure the execution time of partial snippets or a few functions, then we can inject all timing points into our code manually by leveraging Timer:


import time

from auto_profiler import Timer, Tree

def main():
    t = Timer('sleep1', parent_name='main').start()

    t = Timer('sleep2', parent_name='main').start()


if __name__ == '__main__':

Run the example code:

$ python

and it will show you the profiling result:

2.503s  main
├── 1.001s  sleep1
└── 1.501s  sleep2

Supported frameworks

While you can do profiling on normal Python code, as a web developer, chances are that you will usually do profiling on web service code.

Currently supported web frameworks:


For profiling web service code (involving web requests), check out examples.



Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for auto-profiler, version 1.4
Filename, size File type Python version Upload date Hashes
Filename, size auto_profiler-1.4-py2.py3-none-any.whl (15.5 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size auto_profiler-1.4-py3-none-any.whl (15.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size auto_profiler-1.4.tar.gz (8.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page