Skip to content

A timer for profiling a Python function or snippet.

Notifications You must be signed in to change notification settings

RussellLuo/pyprof-timer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyprof-timer

A timer for profiling a Python function or snippet.

Installation

Release version:

$ pip install pyprof-timer

Development version:

$ pip install -e git+https://github.com/RussellLuo/pyprof-timer.git#egg=pyprof-timer

Quick start

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:

# manual_example.py

import time

from pyprof_timer import Timer, Tree


def main():
    t = Timer('sleep1', parent_name='main').start()
    time.sleep(1)
    t.stop()

    t = Timer('sleep2', parent_name='main').start()
    time.sleep(1.5)
    t.stop()

    print(Tree(Timer.root))


if __name__ == '__main__':
    main()

Run the example code:

$ python manual_example.py

and it will show you the profiling result:

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

Automatic 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:

# automatic_example.py

import time # line number 1

from pyprof_timer import Profiler, Tree


def f1():
    time.sleep(1)


def f2():
    time.sleep(1.5)


def show(p):
    print(Tree(p.root))


@Profiler(depth=2, on_disable=show)
def main():
    f1()
    f2()


if __name__ == '__main__':
    main()

Run the example code:

$ python automatic_example.py

and it will show you the profiling result:

2.506s  main  [automatic_example.py:18]
├── 1.001s  f1  [automatic_example.py:6]
│   └── 1.001s  <time.sleep>
└── 1.505s  f2  [automatic_example.py:10]
    └── 1.505s  <time.sleep>

NOTE: If the parameter depth is missing, the profiling output will be overwhelming and the runtime overhead will be high.

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:

Examples

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

License

MIT

About

A timer for profiling a Python function or snippet.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published