⚡️ Speed up function get_fps_if_tick_happens_now by 8%
#622
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 8% (0.08x) speedup for
get_fps_if_tick_happens_nowininference/core/interfaces/camera/video_source.py⏱️ Runtime :
44.3 microseconds→41.1 microseconds(best of563runs)📝 Explanation and details
The optimized version achieves a 7% speedup through several micro-optimizations that reduce attribute lookups and redundant computations:
Key Optimizations:
Cached attribute access:
all_timestamps = fps_monitor.all_timestampseliminates repeated attribute lookups. The original code accessed this attribute twice (lines with 23.8% and 13.5% of execution time), while the optimized version caches it once.Cached length calculation:
ts_len = len(all_timestamps)avoids recalculating the length. The original code calledlen()twice - once for the condition check and again in the return statement.Eliminated intermediate variable: Removed
min_reader_timestampandepsilonvariables, directly usingall_timestamps[0]and the constant1e-8in the calculation.Reused cached values: The return statement uses
ts_len + 1instead of recalculatinglen(fps_monitor.all_timestamps) + 1.Performance Impact:
The optimizations show consistent improvements across all test cases, with particularly strong gains in:
These micro-optimizations are especially effective because this function is likely called frequently in video processing pipelines, where even small per-call improvements compound significantly over time.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
inference/unit_tests/core/interfaces/camera/test_video_source.py::test_get_fps_if_tick_happens_now_when_monitor_has_no_ticks_registeredinference/unit_tests/core/interfaces/camera/test_video_source.py::test_get_fps_if_tick_happens_now_when_monitor_has_tick_registered🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_fps_if_tick_happens_now-mhbqq3etand push.