# HG changeset patch # User Pierre-Yves David # Date 2023-06-12 16:04:09 # Node ID 28620be88da97290f38e7226a8ae643d885587d1 # Parent 3ce370a00225dc8f05b8d119f29c6fd7422c3c20 perf: add a new "context" argument to timer This allow to simple setup/teardown outside of the timed section. Especially using object that need context manager, like a temporary files. diff --git a/contrib/perf.py b/contrib/perf.py --- a/contrib/perf.py +++ b/contrib/perf.py @@ -532,10 +532,16 @@ DEFAULTLIMITS = ( ) +@contextlib.contextmanager +def noop_context(): + yield + + def _timer( fm, func, setup=None, + context=noop_context, title=None, displayall=False, limits=DEFAULTLIMITS, @@ -551,14 +557,16 @@ def _timer( for i in range(prerun): if setup is not None: setup() - func() + with context(): + func() keepgoing = True while keepgoing: if setup is not None: setup() - with profiler: - with timeone() as item: - r = func() + with context(): + with profiler: + with timeone() as item: + r = func() profiler = NOOPCTX count += 1 results.append(item[0])