Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 73 additions & 33 deletions Doc/library/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,53 +235,54 @@ functions:

and gathers profiling statistics as in the :func:`run` function above.

.. class:: Profile(timer=None, timeunit=0.0, subcalls=True, builtins=True)
Both modules also define a :class:`Profile` class that provides more
control over profiling than the :func:`run` function.
You can, for example, format profile results without writing the profile data
to a file::

This class is normally only used if more precise control over profiling is
needed than what the :func:`cProfile.run` function provides.
import profile, pstats, io
pr = profile.Profile()
pr.run('sum(range(1000000))')
s = io.StringIO()
sortby = pstats.SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())

A custom timer can be supplied for measuring how long code takes to run via
the *timer* argument. This must be a function that returns a single number
representing the current time. If the number is an integer, the *timeunit*
specifies a multiplier that specifies the duration of each unit of time. For
example, if the timer returns times measured in thousands of seconds, the
time unit would be ``.001``.
The :class:`Profile` class can also be used as a context manager (see
:ref:`typecontextmanager`)::

Directly using the :class:`Profile` class allows formatting profile results
without writing the profile data to a file::
import cProfile

import cProfile, pstats, io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()
with cProfile.Profile() as pr:
# ... do something ...
pr.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())

The :class:`Profile` class can also be used as a context manager (see
:ref:`typecontextmanager`)::
pr.print_stats()

import cProfile
.. versionchanged:: 3.8
Added context manager support.

with cProfile.Profile() as pr:
# ... do something ...
.. class:: Profile(timer=None, bias=None)

pr.print_stats()
A custom timer can be supplied for measuring how long code takes to run via
the *timer* argument. See :ref:`Using a custom timer <profile-timers>` for
details.

.. versionchanged:: 3.8
Added context manager support.
A custom bias setting can be supplied to aid calibration. See
:ref:`Calibration <profile-calibration>` for more information.

.. method:: enable()
.. method:: run(cmd)

Start collecting profiling data.
Profile the cmd via :func:`exec`.

.. method:: disable()
.. method:: runctx(cmd, globals, locals)

Stop collecting profiling data.
Profile the cmd via :func:`exec` with the specified global and
local environment.

.. method:: runcall(func, *args, **kwargs)

Profile ``func(*args, **kwargs)``

.. method:: create_stats()

Expand All @@ -297,6 +298,29 @@ functions:

Write the results of the current profile to *filename*.

.. class:: cprofile.Profile(timer=None, timeunit=0.0, subcalls=True, builtins=True)

This class allows for profiling all code that executes between the
invocation of the :meth:`enable` and :meth:`disable` methods::

pr = cProfile.Profile()
pr.enable()
# ... do something ...
pr.disable()
print(pr.print_stats())

A custom timer can be supplied for measuring how long code takes to run via
the *timer* argument. See :ref:`Using a custom timer <profile-timers>` for
details.

.. method:: enable()

Start collecting profiling data.

.. method:: disable()

Stop collecting profiling data.

.. method:: run(cmd)

Profile the cmd via :func:`exec`.
Expand All @@ -310,6 +334,22 @@ functions:

Profile ``func(*args, **kwargs)``

.. method:: create_stats()

Stop collecting profiling data and record the results internally
as the current profile.

.. method:: print_stats(sort=-1)

Create a :class:`~pstats.Stats` object based on the current
profile and print the results to stdout. This method will raise
``TypeError`` if none of the :meth:`run`, :meth:`runctx`,
:meth:`runcall`, or :meth:`enable` methods have been called first.

.. method:: dump_stats(filename)

Write the results of the current profile to *filename*.

Note that profiling will only work if the called command/function actually
returns. If the interpreter is terminated (e.g. via a :func:`sys.exit` call
during the called command/function execution) no profiling results will be
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Document the differences between profile.Profile and cProfile.Profile