From 5214bba49aa80d9548db8d398307023dfbe48681 Mon Sep 17 00:00:00 2001 From: mgtm98 Date: Fri, 8 Nov 2024 23:00:28 +0200 Subject: [PATCH] Adding code profiler into our code base --- jac/jaclang/utils/profiler.py | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 jac/jaclang/utils/profiler.py diff --git a/jac/jaclang/utils/profiler.py b/jac/jaclang/utils/profiler.py new file mode 100644 index 0000000000..08ab547ad8 --- /dev/null +++ b/jac/jaclang/utils/profiler.py @@ -0,0 +1,41 @@ +import time +from functools import wraps +from typing import Any, Callable, Dict + + +class CodeProfiler: + + markers: Dict[str, float] = {} + + @staticmethod + def time_function(func: Callable): + """Measure and print the execution time of a function.""" + + @wraps(func) + def wrapper(*args, **kwargs) -> Any: + start_time = time.perf_counter() + result = func(*args, **kwargs) + end_time = time.perf_counter() + print( + f"Function '{func.__name__}' took {end_time - start_time:.6f} seconds" + ) + return result + + return wrapper + + @staticmethod + def start_marker(marker_name: str) -> None: + """Start a named marker to measure a specific section of code.""" + if marker_name in CodeProfiler.markers: + print(f"Warning: Marker '{marker_name}' is already running.") + CodeProfiler.markers[marker_name] = time.perf_counter() + + @staticmethod + def end_marker(marker_name: str) -> None: + """End the named marker and print the time taken since it started.""" + if marker_name not in CodeProfiler.markers: + print(f"Error: Marker '{marker_name}' does not exist.") + return + start_time = CodeProfiler.markers.pop(marker_name) + end_time = time.perf_counter() + print(f"Marker '{marker_name}' took {end_time - start_time:.6f} seconds")