profiling in python

6
PROFILING IN PYTHON John Monday, August 29, 2022

Upload: john-zhang

Post on 23-Jun-2015

141 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Profiling in python

PROFILING IN PYTHON

JohnThursday, April 13, 2023

Page 2: Profiling in python

introduction to the profiler

• A profiler is a program descibes the run time performance of a program.

• Python standard library provide 3 difference profiler:– cProfile : is recommende for most users.– profile: a pure python module whose interface

imitated by cProfile– hotshot: an experimental C module that focused

on minimizing the overhead of profiling

Page 3: Profiling in python

Quick example

Profile an application1. use command line:$ python -m cProfile myscript.py

2. or add into code with main function:impor cProfilecProfile.run(‘main()’)

The result looks like:

Page 4: Profiling in python

Cython profiling basic

Enable it for source file• profiling can be enabled by

adding at the top of file#cython: profile=True• Use a special decorator

disable profiling for one function only:

Use pstats module review the profile

import pstatsp = pstats.Stats(‘fooprof’)p.strip_dirs().sort_stats(‘time’).print_stats()

cimport [email protected](False)def my_func():

pass

Page 5: Profiling in python
Page 6: Profiling in python

Reference

• http://docs.python.org/2/library/profile.html