Skip to content
Open
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
11 changes: 6 additions & 5 deletions timebudget/timebudget.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def my_possibly_slow_function(*args):
import time
from typing import Callable, Optional, Union
import warnings
from threading import get_ident

__all__ = [
'timebudget',
Expand Down Expand Up @@ -67,24 +68,24 @@ def _print(self, msg:str):
self.out_stream.flush()

def start(self, block_name:str):
if block_name in self.start_times:
if (block_name, get_ident()) in self.start_times:
# End should clear out the record, so something odd has happened here.
# try/finally should prevent this, but sometimes it doesn't.
warnings.warn(f"timebudget is confused: timebudget.start({block_name}) without end")
self.start_times[block_name] = time.time()
self.start_times[(block_name, get_ident())] = time.time()

def end(self, block_name:str, quiet:Optional[bool]=None) -> float:
"""Returns number of ms spent in this block this time.
"""
if quiet is None:
quiet = self.quiet_mode
if block_name not in self.start_times:
if (block_name, get_ident()) not in self.start_times:
warnings.warn(f"timebudget is confused: timebudget.end({block_name}) without start")
return float('NaN')
elapsed = 1000*(time.time() - self.start_times[block_name])
elapsed = 1000*(time.time() - self.start_times[(block_name, get_ident())])
self.elapsed_total[block_name] += elapsed
self.elapsed_cnt[block_name] += 1
del self.start_times[block_name]
del self.start_times[(block_name, get_ident())]
if not quiet:
self._print(f"{block_name} took {ms_format(elapsed)}")
return elapsed
Expand Down