You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
⚡️ Speed up function format_time by 97% in PR #294 (add-timing-info-to-generated-tests)
Certainly! Here is an optimized version of your program, keeping the function signature and return identical, but reducing the runtime by.
- **Inlining helpers**: Removes function call overhead for `count_significant_digits`/`format_with_precision`.
- **Avoiding `str(abs(num))`**: Uses math for significant digit checks (`if num >= 100`), since you only compare if it is >= 3 digits.
- **Avoiding unnecessary assignments**: Avoids repeated string formatting expressions.
- **Reduced code nesting**: Reduces the block levels for slightly improved branch prediction.
- **No use of internal lists or expensive function calls in the critical path.**
This should reduce the overall call count, remove slow string-conversion logic, and eliminate small value checks that are now obviously constant-time.
### Key improvements
- No inner functions or per-call string allocations for digit counting.
- Direct math comparison for integer digit checks.
- Calls to string formatting are done only if needed with minimized branching.
This version will be measurably faster per your line statistics, especially on the now inlined "digit count" and "format" logic.
0 commit comments