pythonprofilingresolutiontimingcprofile

Increase Output Resolution of Python cProfile (More Resolution in Output Required)


I am profiling a simple Python function such as

def prof_func():
    x = 100
    y = 100
    a = np.random.rand(x,y)
    c = signal.convolve2d(a, a, boundary='symm', mode='same')

using cProfile but the times I get out only have a resolution of three decimal places. How can I have cProfile print a greater number of decimal places so I can see how long the a = np.random.rand(x,y) line takes. Currently cProfile tells me that it takes 0.000s.

I had hoped that there would be an easy way to increase the resolution of the cProfile output but I have checked the documentation and not found one: https://docs.python.org/3/library/profile.html


Solution

  • The workaround I used to get more significant figures using cprofile and pstats. The pstats.Stats.print_stats() method uses a function called f8 (see 1), that returns the numbers with a fixed format. By defining a similar function in your script where the profiling is made, such as:

    def f8_alt(x):
        return "%14.9f" % x
    

    and monkey patching the static method

    pstats.f8 = f8_alt
    

    the print_stats() method will return the output with more decimals.

    Hope it helps!

    https://github.com/python/cpython/blob/main/Lib/pstats.py