From 0725cd00ec0c1826814b794ef90e4919186cb180 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 21:47:45 +0000 Subject: [PATCH] Optimize format_runtime_comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves a **10% reduction in runtime** (from 1.93ms to 1.75ms) by restructuring the `format_time` function to minimize floating-point operations and improve branch prediction. **Key optimizations:** 1. **Direct threshold comparisons**: Instead of computing intermediate float values (`value = nanoseconds / 1_000`) and then checking thresholds on that value, the optimized version checks raw nanosecond thresholds directly (e.g., `nanoseconds < 10_000` instead of `value < 10`). This avoids unnecessary division operations when they won't be used in the final format string. 2. **Integer division for whole numbers**: When formatting doesn't require decimal places (e.g., "123μs" vs "1.23μs"), the optimized version uses integer division (`//`) instead of float division (`/`), which is faster and avoids float-to-int conversion overhead. 3. **Eliminated conditional expressions**: The original code used nested ternary operators (`f"{value:.2f}μs" if value < 10 else ...`), which require evaluating the condition twice (once for the threshold, once for the format string). The optimized version uses explicit if-statements with direct return paths, improving branch prediction and reducing repeated comparisons. **Performance impact by test case:** - The largest gains appear in the `test_large_scale_many_calls_return_valid_strings` test (12.1% faster), which makes 1000 format calls with varying magnitudes. This demonstrates the cumulative benefit when `format_time` is called repeatedly. - Most individual test cases show 2-8% improvements, confirming consistent gains across different input ranges (nanoseconds, microseconds, milliseconds, seconds). - The optimization is particularly effective for values in the microsecond range (most common in the test data), where the original code performed the most redundant float divisions. **Why this matters:** Line profiler data shows that the original code spent 31.8% of `format_time` execution time on the microsecond formatting line alone (the ternary expression). The optimized version distributes this work across more efficient branches, reducing per-hit time from 505.4ns to individual branch costs of 124-306ns. The function is likely called in performance-sensitive contexts (formatting profiling results, logging), so even a 10% improvement compounds when called thousands of times during analysis workflows. --- codeflash/code_utils/time_utils.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/codeflash/code_utils/time_utils.py b/codeflash/code_utils/time_utils.py index e1a3d4a0e..0b9d7589d 100644 --- a/codeflash/code_utils/time_utils.py +++ b/codeflash/code_utils/time_utils.py @@ -74,13 +74,22 @@ def format_time(nanoseconds: int) -> str: if nanoseconds < 1_000: return f"{nanoseconds}ns" if nanoseconds < 1_000_000: - value = nanoseconds / 1_000 - return f"{value:.2f}μs" if value < 10 else (f"{value:.1f}μs" if value < 100 else f"{int(value)}μs") + if nanoseconds < 10_000: + return f"{nanoseconds / 1_000:.2f}μs" + if nanoseconds < 100_000: + return f"{nanoseconds / 1_000:.1f}μs" + return f"{nanoseconds // 1_000}μs" if nanoseconds < 1_000_000_000: - value = nanoseconds / 1_000_000 - return f"{value:.2f}ms" if value < 10 else (f"{value:.1f}ms" if value < 100 else f"{int(value)}ms") - value = nanoseconds / 1_000_000_000 - return f"{value:.2f}s" if value < 10 else (f"{value:.1f}s" if value < 100 else f"{int(value)}s") + if nanoseconds < 10_000_000: + return f"{nanoseconds / 1_000_000:.2f}ms" + if nanoseconds < 100_000_000: + return f"{nanoseconds / 1_000_000:.1f}ms" + return f"{nanoseconds // 1_000_000}ms" + if nanoseconds < 10_000_000_000: + return f"{nanoseconds / 1_000_000_000:.2f}s" + if nanoseconds < 100_000_000_000: + return f"{nanoseconds / 1_000_000_000:.1f}s" + return f"{nanoseconds // 1_000_000_000}s" def format_perf(percentage: float) -> str: