Archive - Originally posted on "The Horse's Mouth" - 2016-11-01 07:46:03 - Graham Ellis
Python's profiler lets you run code and log statistics, and the pstats module allows you to anaylse the resulting statistics. New example from last week's Intermediate Python Course is [here].
import cProfile
import pstats
import railway
cProfile.run("railway.test_code()","profile.out")
result = pstats.Stats("profile.out")
result.sort_stats("ncalls")
result.print_stats()
result.print_callees()
As ever with Python, the code example is largely self-describing ... take a look at the source (linked above) to show you some sample output too. The call being profiled is [here] amd the data may be viewed [here]. Data can be downloaded from [here] (that way tabs and spaces come through correct if you want to run the program!)