"""Profiling utilities for solvers."""importtimefromfunctoolsimportwrapsfromtypingimportTYPE_CHECKING,CallableifTYPE_CHECKING:from.baseimportBaseSolverProfilingStats=dict[str,dict[str,float]]
[docs]deftimeit(func:Callable)->Callable:""" Decorator to time functions only if profiling is enabled. Args: func: The function to time. Returns: A wrapped function that records timing stats to solver.profiling_stats. """@wraps(func)deftimeit_wrapper(solver:"BaseSolver",*args,**kwargs):# Skip timing overhead if profiling is disabledifnotsolver.profiling:returnfunc(solver,*args,**kwargs)start=time.perf_counter()try:returnfunc(solver,*args,**kwargs)finally:stats=solver.profiling_stats[func.__name__]stats["time"]+=time.perf_counter()-startstats["calls"]+=1returntimeit_wrapper