user1767754
user1767754

Reputation: 25094

How to get more granular results with cProfile

I have a little snippet which I'm profiling, however the list comprehension is listed as one item in the calls

import fileinput
import cProfile

pr = cProfile.Profile()
pr.enable()
x = [float(i) for i in range(0, 10**8)]
pr.disable()
pr.print_stats(sort='time')

Output:

     2 function calls in 19.769 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1   19.769   19.769   19.769   19.769 sandboxMinimalCProfile.py:6(<listcomp>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Is it possible to get for example what the cost was to convert to float ?

Upvotes: 2

Views: 360

Answers (1)

Acorn
Acorn

Reputation: 26066

The issue here is that float is a builtin, so they don't count as calls for the profiler.

You can try to workaround it; however, the profiler is not designed for micro-benchmarks in any case. Instead, try the timeit standard module:

$ python3 -m timeit 'float(1000000)'
10000000 loops, best of 3: 0.118 usec per loop

Upvotes: 1

Related Questions