profile
optimus_dl.core.profile
¶
Performance profiling utilities.
This module provides functions for measuring execution time of code blocks, iterators, and function calls. Supports both CPU timing (using perf_counter) and GPU timing (using CUDA events for accurate GPU kernel timing).
measured_iter(itr)
¶
Measure time between iterations of an iterator.
Yields tuples of (elapsed_time_ms, element) for each element in the iterator. The elapsed time is measured from the start of one iteration to the start of the next, providing per-iteration timing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
itr
|
Iterator[T]
|
Iterator to measure. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Tuples of (elapsed_time_ms, element) where elapsed_time_ms is the |
T
|
time in milliseconds since the previous iteration. |
Example
Source code in optimus_dl/core/profile.py
measured_lambda(f, cuda_events=False, enabled=True)
¶
Measure execution time of a callable function.
Supports both CPU timing (using perf_counter) and GPU timing (using CUDA events). CUDA events provide more accurate timing for GPU operations as they measure actual GPU kernel execution time rather than wall-clock time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[], T]
|
Callable function to measure (takes no arguments). |
required |
cuda_events
|
bool
|
If True, use CUDA events for timing (more accurate for GPU operations). If False, use CPU perf_counter. Requires CUDA to be available. |
False
|
enabled
|
bool
|
If False, skip timing and return (0, result). Useful for disabling profiling in production code. |
True
|
Returns:
| Type | Description |
|---|---|
float
|
Tuple of (elapsed_time_ms, result) where: |
T
|
|
tuple[float, T]
|
|
Example
Source code in optimus_dl/core/profile.py
measured_next(itr)
¶
Measure time to get the next element from an iterator.
This is useful for measuring data loading time, as it measures the time to fetch a single batch from a data iterator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
itr
|
Iterator[T]
|
Iterator to get next element from. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Tuple of (elapsed_time_ms, element) where elapsed_time_ms is the |
T
|
time in milliseconds to get the next element. |