I have a class Foo and it implements a method compute. I would like to see how long compute takes and where it spends most of its time when executing. To resolve the first problem, I used timeit.default_timer in a loop and averaged:
import numpy as np from timeit import default_timer as Timer foo = Foo() n = int(10e8) times = np.empty(n) for i in range(n): start = Timer() foo.compute(i) times[i] = Timer() - start print(np.sum(times) / times.size) This tells me what the average execution time of each compute call was, easy enough; except its slower than I thought.
Now I would like to break down the execution profile of compute, but the followingn doesn't seem to do the trick:
import cProfile for i in range(n): cProfile.run(foo.compute(i)) I didn't think it would work anyhow, but the docs seem to suggest I would have to put this loop in a method and then profile that loop? So I tried that but it doesn't show whats going on inside compute which is what I desire. How can this be accomplished?
cProfileoutput for every single iteration?computehas several functions inside of it, so I want to determine which is taking the longest in a "real world" scenario.foo.computebehaves wildly different depending on input, profiling the loop and merely ignoring the outermost layer should be appropriate.