ps.print_stats()
print(s.getvalue())
- The :class:`Profile` class can also be used as a context manager (see
- :ref:`typecontextmanager`)::
+ The :class:`Profile` class can also be used as a context manager (supported
+ only in :mod:`cProfile` module. see :ref:`typecontextmanager`)::
import cProfile
.. method:: enable()
- Start collecting profiling data.
+ Start collecting profiling data. Only in :mod:`cProfile`.
.. method:: disable()
- Stop collecting profiling data.
+ Stop collecting profiling data. Only in :mod:`cProfile`.
.. method:: create_stats()
relative indications of where time is being spent.
In Python, since there is an interpreter active during execution, the presence
-of instrumented code is not required to do deterministic profiling. Python
-automatically provides a :dfn:`hook` (optional callback) for each event. In
-addition, the interpreted nature of Python tends to add so much overhead to
+of instrumented code is not required in order to do deterministic profiling.
+Python automatically provides a :dfn:`hook` (optional callback) for each event.
+In addition, the interpreted nature of Python tends to add so much overhead to
execution, that deterministic profiling tends to only add small processing
overhead in typical applications. The result is that deterministic profiling is
not that expensive, yet provides extensive run time statistics about the
*who* parameter should be specified using one of the :const:`RUSAGE_\*`
constants described below.
+ A simple example::
+
+ from resource import *
+ import time
+
+ # a non CPU-bound task
+ time.sleep(3)
+ print(getrusage(RUSAGE_SELF))
+
+ # a CPU-bound task
+ for i in range(10 ** 8):
+ _ = 1 + 1
+ print(getrusage(RUSAGE_SELF))
+
The fields of the return value each describe how a particular system resource
has been used, e.g. amount of time spent running is user mode or number of times
the process was swapped out of main memory. Some values are dependent on the
remaining values are integers. Consult the :manpage:`getrusage(2)` man page for
detailed information about these values. A brief summary is presented here:
- +--------+---------------------+-------------------------------+
- | Index | Field | Resource |
- +========+=====================+===============================+
- | ``0`` | :attr:`ru_utime` | time in user mode (float) |
- +--------+---------------------+-------------------------------+
- | ``1`` | :attr:`ru_stime` | time in system mode (float) |
- +--------+---------------------+-------------------------------+
- | ``2`` | :attr:`ru_maxrss` | maximum resident set size |
- +--------+---------------------+-------------------------------+
- | ``3`` | :attr:`ru_ixrss` | shared memory size |
- +--------+---------------------+-------------------------------+
- | ``4`` | :attr:`ru_idrss` | unshared memory size |
- +--------+---------------------+-------------------------------+
- | ``5`` | :attr:`ru_isrss` | unshared stack size |
- +--------+---------------------+-------------------------------+
- | ``6`` | :attr:`ru_minflt` | page faults not requiring I/O |
- +--------+---------------------+-------------------------------+
- | ``7`` | :attr:`ru_majflt` | page faults requiring I/O |
- +--------+---------------------+-------------------------------+
- | ``8`` | :attr:`ru_nswap` | number of swap outs |
- +--------+---------------------+-------------------------------+
- | ``9`` | :attr:`ru_inblock` | block input operations |
- +--------+---------------------+-------------------------------+
- | ``10`` | :attr:`ru_oublock` | block output operations |
- +--------+---------------------+-------------------------------+
- | ``11`` | :attr:`ru_msgsnd` | messages sent |
- +--------+---------------------+-------------------------------+
- | ``12`` | :attr:`ru_msgrcv` | messages received |
- +--------+---------------------+-------------------------------+
- | ``13`` | :attr:`ru_nsignals` | signals received |
- +--------+---------------------+-------------------------------+
- | ``14`` | :attr:`ru_nvcsw` | voluntary context switches |
- +--------+---------------------+-------------------------------+
- | ``15`` | :attr:`ru_nivcsw` | involuntary context switches |
- +--------+---------------------+-------------------------------+
+ +--------+---------------------+---------------------------------------+
+ | Index | Field | Resource |
+ +========+=====================+=======================================+
+ | ``0`` | :attr:`ru_utime` | time in user mode (float seconds) |
+ +--------+---------------------+---------------------------------------+
+ | ``1`` | :attr:`ru_stime` | time in system mode (float seconds) |
+ +--------+---------------------+---------------------------------------+
+ | ``2`` | :attr:`ru_maxrss` | maximum resident set size |
+ +--------+---------------------+---------------------------------------+
+ | ``3`` | :attr:`ru_ixrss` | shared memory size |
+ +--------+---------------------+---------------------------------------+
+ | ``4`` | :attr:`ru_idrss` | unshared memory size |
+ +--------+---------------------+---------------------------------------+
+ | ``5`` | :attr:`ru_isrss` | unshared stack size |
+ +--------+---------------------+---------------------------------------+
+ | ``6`` | :attr:`ru_minflt` | page faults not requiring I/O |
+ +--------+---------------------+---------------------------------------+
+ | ``7`` | :attr:`ru_majflt` | page faults requiring I/O |
+ +--------+---------------------+---------------------------------------+
+ | ``8`` | :attr:`ru_nswap` | number of swap outs |
+ +--------+---------------------+---------------------------------------+
+ | ``9`` | :attr:`ru_inblock` | block input operations |
+ +--------+---------------------+---------------------------------------+
+ | ``10`` | :attr:`ru_oublock` | block output operations |
+ +--------+---------------------+---------------------------------------+
+ | ``11`` | :attr:`ru_msgsnd` | messages sent |
+ +--------+---------------------+---------------------------------------+
+ | ``12`` | :attr:`ru_msgrcv` | messages received |
+ +--------+---------------------+---------------------------------------+
+ | ``13`` | :attr:`ru_nsignals` | signals received |
+ +--------+---------------------+---------------------------------------+
+ | ``14`` | :attr:`ru_nvcsw` | voluntary context switches |
+ +--------+---------------------+---------------------------------------+
+ | ``15`` | :attr:`ru_nivcsw` | involuntary context switches |
+ +--------+---------------------+---------------------------------------+
This function will raise a :exc:`ValueError` if an invalid *who* parameter is
specified. It may also raise :exc:`error` exception in unusual circumstances.