grains.profiling.profile¶
-
grains.profiling.
profile
(output_format='html', output_filename=None, html_open=False)[source]¶ Profiles a block of code with Pyinstrument.
Intended to be used in a with statement.
- Parameters
output_format ({‘html’, ‘text’}, optional) – Shows the result either as text or in an HTML file. If
output_format = 'html'
, the file is saved according to theoutput_filename
parameter. The default is ‘html’.output_filename (str, optional) – Only taken into account if
output_format = 'html'
. If not given (default), the html output is saved to the same directory the caller resides. The name of the html file is the same as that of the caller.html_open (bool, optional) – Only taken into account if
output_format = 'html'
. If True, the generated HTML file is opened in the default browser. The default is False.
Notes
In the implementation, we move two levels up in the stack frame, one for exiting the context manager and one for exiting this generator. This assumes that
profile()
was called as a context manager. As a good practice, provide theoutput_filename
input argument.Examples
Measure the time needed to generate 1 million uniformly distributed random numbers.
>>> import random >>> with profile('html') as p: ... for _ in range(1000000): ... rand_num = random.uniform(1, 2.2)