⚡️ Speed up function fmt_delta by 11% in PR #1941 (cf-compare-copy-benchmarks)#1943
Conversation
The optimized code replaces f-string formatting (`f"[green]{pct:+.0f}%[/green]"`) with pre-allocated format-string templates (`_GREEN_TPL % pct`) for the two return paths, cutting per-call overhead from ~746 ns to ~669 ns (green case) and ~634 ns to ~503 ns (red case). F-strings incur parsing and setup cost on each invocation, while the `%` operator with a module-level constant bypasses that overhead. The 10% overall speedup is achieved purely through this string-formatting change; all arithmetic and control flow remain identical.
|
Claude finished @codeflash-ai[bot]'s task in 2m 21s —— View job PR Review SummaryPrek ChecksAll checks pass — Code ReviewSMALL PR — optimization correctness check. The change introduces two module-level template constants and replaces f-string formatting with _GREEN_TPL = "[green]%+.0f%%[/green]"
_RED_TPL = "[red]%+.0f%%[/red]"The No bugs, security issues, or breaking changes. Duplicate DetectionNo duplicates detected. Test Coverage977 generated regression tests pass at 100% coverage. No existing unit tests exist for this function. CI note: The Last updated: 2026-04-01 |
|
CI failures are pre-existing on the base branch |
|
CI failures are pre-existing on the base branch (not caused by this PR): unit-tests (all Python versions), async-optimization, bubble-sort-optimization-unittest, init-optimization, js-cjs-function-optimization, js-ts-class-optimization. The same failures are present on PR #1941 (cf-compare-copy-benchmarks). Leaving open for merge once base branch CI is fixed. |
|
CI failures are pre-existing on the base branch (not caused by this PR): |
|
CI failures are in trace benchmark tests and appear to be flaky test pollution unrelated to this optimization change. Leaving open for merge once CI stabilizes. |
|
CI failures are pre-existing on the base branch (not caused by this PR): code/snyk (test limit), js-ts-class-optimization, unit-tests (all versions) all fail on base branch cf-compare-copy-benchmarks (PR #1941). Other failures (async-optimization, bubble-sort-optimization-unittest, init-optimization, js-cjs-function-optimization) are likely flaky e2e tests — the optimization itself is a trivial and correct change. Leaving open for merge once base branch CI is fixed. |
|
CI failures are pre-existing on the base branch (not caused by this PR): |
|
The optimization of |
|
CI failures are pre-existing on the base branch (not caused by this PR): unit-tests (all Python versions), unit-tests (windows-latest, 3.13), js-ts-class-optimization. Leaving open for merge once base branch CI is fixed. |
⚡️ This pull request contains optimizations for PR #1941
If you approve this dependent PR, these changes will be merged into the original PR branch
cf-compare-copy-benchmarks.📄 11% (0.11x) speedup for
fmt_deltaincodeflash/benchmarking/compare.py⏱️ Runtime :
579 microseconds→523 microseconds(best of250runs)📝 Explanation and details
The optimized code replaces f-string formatting (
f"[green]{pct:+.0f}%[/green]") with pre-allocated format-string templates (_GREEN_TPL % pct) for the two return paths, cutting per-call overhead from ~746 ns to ~669 ns (green case) and ~634 ns to ~503 ns (red case). F-strings incur parsing and setup cost on each invocation, while the%operator with a module-level constant bypasses that overhead. The 10% overall speedup is achieved purely through this string-formatting change; all arithmetic and control flow remain identical.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1941-2026-04-01T14.15.33and push.