qrunch.common.execution_timer
Provides functionality to measure and report the execution time.
using a singleton timer and context manager. It includes two main classes: ExecutionTimer and TimerContext.
The ExecutionTimer class is a singleton maintaining a registry of timing sections, their durations, and hierarchical contexts. It supports starting and stopping timers, adding and removing context, and reporting accumulated timings.
The TimerContext class is a context manager designed to handle the timing of a specific code section, integrating with the ExecutionTimer to track the start and stop times.
Example usage: .. code-block:: python
- with TimerContext(“section_name”):
# code block to measure
The full name of the timing section will reflect nested contexts, providing a detailed breakdown of execution times.
Functions
|
Get the report of execution times of all parts of the run; flatten counters if requested. |
|
Get the execution times of all parts of the run; flatten counters if requested. |
|
Compute total elapsed time inside the specified category. |
Classes
Define category choices. |
|
Context manager for timing a specific section of code. |
|
Typed dictionary to hold execution timings for various parts of a computation. |
- class ComputationalCategory
Bases:
EnumDefine category choices.
- CLASSICAL = 'classical'
- QUANTUM = 'quantum'
- class TimerContext
Bases:
objectContext manager for timing a specific section of code.
- __init__(name: str, category: ComputationalCategory = ComputationalCategory.CLASSICAL, counter: int | None = None) None
Initialize the context manager with a section name.
- Parameters:
name (str) – The name of the timing section.
category (ComputationalCategory) – Select Quantum (including Simulated Quantum) or Classical Computing
counter (int | None) – An optional counter (for instance an interation counter)
- Return type:
None
- class TimingReport
Bases:
TypedDictTyped dictionary to hold execution timings for various parts of a computation.
- __init__(*args, **kwargs)
- classmethod __new__(*args, **kwargs)
- category: str
- clear() None. Remove all items from D.
- copy() a shallow copy of D
- classmethod fromkeys(iterable, value=None, /)
Create a new dictionary with keys from iterable and values set to value.
- get(key, default=None, /)
Return the value for key if key is in the dictionary, else default.
- items() a set-like object providing a view on D's items
- keys() a set-like object providing a view on D's keys
- pop(k[, d]) v, remove specified key and return the corresponding value.
If the key is not found, return the default if given; otherwise, raise a KeyError.
- popitem()
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- setdefault(key, default=None, /)
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- time: float
- update([E, ]**F) None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
- values() an object providing a view on D's values
- get_execution_times_report(*, flatten: bool = True) str
Get the report of execution times of all parts of the run; flatten counters if requested.
- Parameters:
flatten (bool) – True, then flatten all counters into one.
- Return type:
str
- get_execution_timings(*, flatten: bool = True) dict[tuple[str, ...], TimingReport]
Get the execution times of all parts of the run; flatten counters if requested.
- Parameters:
flatten (bool) – True, then flatten all counters into one.
- Return type:
dict[tuple[str, …], TimingReport]
- get_total_category_time(*, category: Literal['quantum', 'classical']) float
Compute total elapsed time inside the specified category.
- Parameters:
category (Literal['quantum', 'classical']) – The category to report timings for.
- Return type:
float