Today I will show how to use the pstats python module.

It’s just some simple example, but can be useful for us.

Let’s add some source code to test this python module.

The documentation tells us:

A profiler is a program that describes the run time performance of a program, providing a variety of statistics.
This documentation describes the profiler functionality provided in the modules cProfile, profile, and pstats.

Let’s make one.

And now will parse the profiler file>

Now we try to see more about Django module.
You can make one python script with the name test-django.py.

Add the next source code:

You can save the file and use the next command:

Go to and run the next python commands in the python3.2 shell:

We see the time to import the Django module is about 0.066 seconds.

All 4 functions it’s called during this period of time.

We sort the profile by cumulative time, and then only prints the ten most significant lines.

The python module can show us what functions were looping a lot, and taking a lot of time.

Try with one more complex example with functions. In this case the output it’s the same.

You can read more about pstats and deterministic profiling of Python programs with all limitation here.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.