The pstats Module

The pstats module is a tool that analyzes data collected by the Python profiler, as Example 11-5 shows.

Example 11-5. Using the pstats Module

File: pstats-example-1.py

import pstats
import profile

def func1():
 for i in range(1000):
 pass

def func2():
 for i in range(1000):
 func1()

p = profile.Profile()
p.run("func2()")

s = pstats.Stats(p)
s.sort_stats("time", "name").print_stats()

 1003 function calls in 1.574 CPU seconds

 Ordered by: internal time, function name

 ncalls tottime percall cumtime percall filename:lineno(function)
 1000 1.522 0.002 1.522 0.002 pstats-example-1.py:4(func1)
 1 0.051 0.051 1.573 1.573 pstats-example-1.py:8(func2)
 1 0.001 0.001 1.574 1.574 profile:0(func2())
 1 0.000 0.000 1.573 1.573 :1(?)
 0 0.000 0.000 profile:0(profiler)

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols

Internationalization

Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules



Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net