Skip to content

Commit 2bffc7f

Browse files
Update codeflash/code_utils/time_utils.py
Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
1 parent 5115295 commit 2bffc7f

File tree

1 file changed

+30
-38
lines changed

1 file changed

+30
-38
lines changed

codeflash/code_utils/time_utils.py

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,44 +53,36 @@ 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-
57-
def count_significant_digits(num: int) -> int:
58-
"""Count significant digits in an integer."""
59-
return len(str(abs(num)))
60-
61-
def format_with_precision(value: float, unit: str) -> str:
62-
"""Format a value with 3 significant digits precision."""
63-
if value >= 100:
64-
return f"{value:.0f}{unit}"
65-
if value >= 10:
66-
return f"{value:.1f}{unit}"
67-
return f"{value:.2f}{unit}"
68-
69-
result = ""
56+
# Inlined significant digit check: >= 3 digits if value >= 100
7057
if nanoseconds < 1_000:
71-
result = f"{nanoseconds}ns"
72-
elif nanoseconds < 1_000_000:
73-
# Convert to microseconds
58+
return f"{nanoseconds}ns"
59+
if nanoseconds < 1_000_000:
7460
microseconds_int = nanoseconds // 1_000
75-
if count_significant_digits(microseconds_int) >= 3:
76-
result = f"{microseconds_int}μs"
77-
else:
78-
microseconds_float = nanoseconds / 1_000
79-
result = format_with_precision(microseconds_float, "μs")
80-
elif nanoseconds < 1_000_000_000:
81-
# Convert to milliseconds
61+
if microseconds_int >= 100:
62+
return f"{microseconds_int}μs"
63+
microseconds = nanoseconds / 1_000
64+
# Format with precision: 3 significant digits
65+
if microseconds >= 100:
66+
return f"{microseconds:.0f}μs"
67+
if microseconds >= 10:
68+
return f"{microseconds:.1f}μs"
69+
return f"{microseconds:.2f}μs"
70+
if nanoseconds < 1_000_000_000:
8271
milliseconds_int = nanoseconds // 1_000_000
83-
if count_significant_digits(milliseconds_int) >= 3:
84-
result = f"{milliseconds_int}ms"
85-
else:
86-
milliseconds_float = nanoseconds / 1_000_000
87-
result = format_with_precision(milliseconds_float, "ms")
88-
else:
89-
# Convert to seconds
90-
seconds_int = nanoseconds // 1_000_000_000
91-
if count_significant_digits(seconds_int) >= 3:
92-
result = f"{seconds_int}s"
93-
else:
94-
seconds_float = nanoseconds / 1_000_000_000
95-
result = format_with_precision(seconds_float, "s")
96-
return result
72+
if milliseconds_int >= 100:
73+
return f"{milliseconds_int}ms"
74+
milliseconds = nanoseconds / 1_000_000
75+
if milliseconds >= 100:
76+
return f"{milliseconds:.0f}ms"
77+
if milliseconds >= 10:
78+
return f"{milliseconds:.1f}ms"
79+
return f"{milliseconds:.2f}ms"
80+
seconds_int = nanoseconds // 1_000_000_000
81+
if seconds_int >= 100:
82+
return f"{seconds_int}s"
83+
seconds = nanoseconds / 1_000_000_000
84+
if seconds >= 100:
85+
return f"{seconds:.0f}s"
86+
if seconds >= 10:
87+
return f"{seconds:.1f}s"
88+
return f"{seconds:.2f}s"

0 commit comments

Comments
 (0)