Skip to content

Commit 80bc4fa

Browse files
⚡️ 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!
1 parent 6e80738 commit 80bc4fa

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

codeflash/code_utils/time_utils.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,33 @@ def humanize_runtime(time_in_ns: int) -> str:
5353

5454
def format_time(nanoseconds: int) -> str:
5555
"""Format nanoseconds into a human-readable string with 3 significant digits when needed."""
56-
# Define conversion factors and units
57-
conversions = [(1_000_000_000, "s"), (1_000_000, "ms"), (1_000, "μs"), (1, "ns")]
58-
59-
# Handle nanoseconds case directly (no decimal formatting needed)
56+
# Fast path for small values
6057
if nanoseconds < 1_000:
6158
return f"{nanoseconds}ns"
6259

63-
# Find appropriate unit
64-
for divisor, unit in conversions:
65-
if nanoseconds >= divisor:
66-
value = nanoseconds / divisor
67-
int_value = nanoseconds // divisor
60+
# Inline conversion check for speed: no for loop
61+
if nanoseconds >= 1_000_000_000:
62+
divisor, unit = 1_000_000_000, "s"
63+
elif nanoseconds >= 1_000_000:
64+
divisor, unit = 1_000_000, "ms"
65+
elif nanoseconds >= 1_000:
66+
divisor, unit = 1_000, "μs"
67+
else:
68+
# Should not happen, fallback to ns
69+
return f"{nanoseconds}ns"
70+
71+
value = nanoseconds / divisor
72+
int_value = nanoseconds // divisor
6873

69-
# Use integer formatting for values >= 100
70-
if int_value >= 100:
71-
formatted_value = str(int_value)
72-
# Format with precision for 3 significant digits
73-
elif value >= 100:
74-
formatted_value = f"{value:.0f}"
75-
elif value >= 10:
76-
formatted_value = f"{value:.1f}"
77-
else:
78-
formatted_value = f"{value:.2f}"
74+
# Use integer formatting for values >= 100
75+
if int_value >= 100:
76+
return str(int_value) + unit
77+
# Format with precision for 3 significant digits
78+
if value >= 100:
79+
return f"{value:.0f}{unit}"
80+
if value >= 10:
81+
return f"{value:.1f}{unit}"
82+
return f"{value:.2f}{unit}"
7983

80-
return f"{formatted_value}{unit}"
8184

82-
# This should never be reached, but included for completeness
83-
return f"{nanoseconds}ns"
85+
_CONVERSION_UNITS = ((1_000_000_000, "s"), (1_000_000, "ms"), (1_000, "μs"))

0 commit comments

Comments
 (0)