Skip to content

feat: Add reset_stats method to LineProfiler for resetting accumulated profiling data #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions line_profiler/_line_profiler.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_explicit_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading