From 550d1aa314ad3d7276b0cae1bcd73e2f51b04cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9B=8F=E4=B8=80?= Date: Wed, 12 Mar 2025 21:17:30 +0800 Subject: [PATCH] Add API reset_stats --- line_profiler/_line_profiler.pyx | 9 +++++++++ tests/test_explicit_profile.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/line_profiler/_line_profiler.pyx b/line_profiler/_line_profiler.pyx index 5438eb83..3983bb20 100644 --- a/line_profiler/_line_profiler.pyx +++ b/line_profiler/_line_profiler.pyx @@ -360,6 +360,15 @@ cdef class LineProfiler: self._c_last_time[threading.get_ident()].clear() unset_trace() + def reset_stats(self): + """ + Reset the currently accumulated timings information. + """ + it = self._c_code_map.begin() + while it != self._c_code_map.end(): + cython.operator.dereference(it).second.clear() + cython.operator.preincrement(it) + def get_stats(self): """ Return a LineStats object containing the timings. diff --git a/tests/test_explicit_profile.py b/tests/test_explicit_profile.py index b76c03ba..100fc90d 100644 --- a/tests/test_explicit_profile.py +++ b/tests/test_explicit_profile.py @@ -25,6 +25,12 @@ def func(a): print(f'profiler.code_hash_map={profiler.code_hash_map}') profiler.print_stats() + profiler.reset_stats() + for code in profiler.code_hash_map: + for entry in profiler.code_hash_map[code]: + assert entry in profiler.c_code_map + assert len(profiler.c_code_map[entry]) == 0 + def _demo_explicit_profile_script(): return ub.codeblock(