Skip to content

Commit

Permalink
update timer
Browse files Browse the repository at this point in the history
  • Loading branch information
lf-zhao committed Dec 24, 2024
1 parent 16e6a79 commit e4ce30e
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions predicators/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3881,18 +3881,34 @@ def log_rich_table(rich_table: rich.table.Table) -> "Texssas":


class Timer:
"""
Timer context manager to measure the execution time of a block of code.
"""

def __init__(self, enable_print=True, name="Block"):
self.enable_print = enable_print
self.name = name
# self.elapsed_time = 0 # Initialize elapsed_time

def __init__(self):
def get_elapsed_time(self):
"""Get the elapsed time in seconds even in the context."""
return time.time() - self.start_time

def __enter__(self):
self.start_time = time.time()
self.start_clock = self._clock()
return self # Returning self to access its attributes

def __exit__(self, exc_type, exc_val, exc_tb):
"""Get the elapsed time when exiting the context."""
self.elapsed_time = self.get_elapsed_time()
if self.enable_print:
print(f"[{self.name}] executed in {self.elapsed_time:.4f} seconds")

def _clock(self):
times = os.times()
return times[0] + times[1]
@staticmethod
def get_current_time():
from datetime import datetime

def __str__(self):
return "[Timer: %.3fs CPU, %.3fs wall-clock]" % (
self._clock() - self.start_clock, time.time() - self.start_time)
return datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]


@contextlib.contextmanager
Expand Down

0 comments on commit e4ce30e

Please sign in to comment.