profile

lsst.utils.timer.profile(filename: Optional[str], log: Optional[logging.Logger] = None) → Iterator[Optional[cProfile.Profile]]

Profile the enclosed code block and save the result to a file.

Parameters:
filename : str or None

Filename to which to write profile (profiling disabled if None or empty string).

log : logging.Logger, optional

Log object for logging the profile operations.

Yields:
prof : cProfile.Profile or None

If profiling is enabled, the context manager returns the cProfile.Profile object (otherwise it returns None), which allows additional control over profiling.

Examples

You can obtain the cProfile.Profile object using the “as” clause, e.g.:

with profile(filename) as prof:
    runYourCodeHere()

The output cumulative profile can be printed with a command-line like:

python -c 'import pstats;             pstats.Stats("<filename>").sort_stats("cumtime").print_stats(30)'