Batman
Batman

Reputation: 8947

Correct Way To Use cProfile

I want to profile some functions that I've written. I'm looking at the docs for cProfile, and I don't understand what (if any) the difference between the following two pieces of code is:

import cProfile

profile = cProfile.Profile()
result = profile.runcall(func, *args, **kwargs)
profile.dump_stats('profile.stats')

and

import cProfile

profile = cProfile.Profile()
profile.enable()
result = func(*args, **kwargs)
profile.disable()
profile.dump_stats('profile.stats')

Are these two blocks equivalent, or are these doing subtly different things?

Upvotes: 1

Views: 527

Answers (1)

viraptor
viraptor

Reputation: 34205

They're almost the same: https://github.com/python/cpython/blob/581eb3e0dc0bc0d306363c8d404ffd9988b0cc87/Lib/cProfile.py#L106

runcall will stop the profiling in case of exceptions, but otherwise it's the same functionality.

Upvotes: 2

Related Questions