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 36% in PR #301 (temp-precommit-fix)
Here's a significantly **faster** version of your function.
**Main bottlenecks:**
- The `conversions` list is being recreated every call (move outside the function).
- Division (and string formatting) is repeated unnecessarily.
- For most inputs, only one conversion is needed, so using `elif` instead of a loop is faster.
- Inlining the logic (removing the for loop) along with minimal branching is much faster.
- Use `str()` formatting (not f-strings) for speed with integers.
Here's a rewritten, optimized version.
**Optimization summary:**
- Conversion table is not reconstructed per call.
- No loop; fastest matching unit is chosen via flat if.
- Common case (<1000) is handled with a very fast path.
- No extra work, all math and comparisons are minimal and streamlined.
**Same output guaranteed, significantly faster runtime for all inputs.**
Let me know if you want it further micro-optimized or vectorized!
0 commit comments