Skip to content

Commit 85c17f4

Browse files
authored
Merge pull request codeflash-ai#538 from codeflash-ai/codeflash/optimize-pr537-2025-07-11T04.11.46
⚡️ Speed up method `CommentMapper.get_comment` by 30% in PR codeflash-ai#537 (`runtime-fixes-jul10`)
2 parents 4396abc + 1a2d472 commit 85c17f4

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

codeflash/code_utils/time_utils.py

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import datetime as dt
24
import re
35

@@ -58,42 +60,27 @@ def format_time(nanoseconds: int) -> str:
5860
raise TypeError("Input must be an integer.")
5961
if nanoseconds < 0:
6062
raise ValueError("Input must be a positive integer.")
61-
conversions = [(1_000_000_000, "s"), (1_000_000, "ms"), (1_000, "μs"), (1, "ns")]
6263

63-
# Handle nanoseconds case directly (no decimal formatting needed)
6464
if nanoseconds < 1_000:
6565
return f"{nanoseconds}ns"
66-
67-
# Find appropriate unit
68-
for divisor, unit in conversions:
69-
if nanoseconds >= divisor:
70-
value = nanoseconds / divisor
71-
int_value = nanoseconds // divisor
72-
73-
# Use integer formatting for values >= 100
74-
if int_value >= 100:
75-
formatted_value = f"{int_value:.0f}"
76-
# Format with precision for 3 significant digits
77-
elif value >= 100:
78-
formatted_value = f"{value:.0f}"
79-
elif value >= 10:
80-
formatted_value = f"{value:.1f}"
81-
else:
82-
formatted_value = f"{value:.2f}"
83-
84-
return f"{formatted_value}{unit}"
85-
86-
# This should never be reached, but included for completeness
87-
return f"{nanoseconds}ns"
66+
if nanoseconds < 1_000_000:
67+
value = nanoseconds / 1_000
68+
return f"{value:.2f}μs" if value < 10 else (f"{value:.1f}μs" if value < 100 else f"{int(value)}μs")
69+
if nanoseconds < 1_000_000_000:
70+
value = nanoseconds / 1_000_000
71+
return f"{value:.2f}ms" if value < 10 else (f"{value:.1f}ms" if value < 100 else f"{int(value)}ms")
72+
value = nanoseconds / 1_000_000_000
73+
return f"{value:.2f}s" if value < 10 else (f"{value:.1f}s" if value < 100 else f"{int(value)}s")
8874

8975

9076
def format_perf(percentage: float) -> str:
9177
"""Format percentage into a human-readable string with 3 significant digits when needed."""
92-
percentage_abs = abs(percentage)
93-
if percentage_abs >= 100:
78+
# Branch order optimized
79+
abs_perc = abs(percentage)
80+
if abs_perc >= 100:
9481
return f"{percentage:.0f}"
95-
if percentage_abs >= 10:
82+
if abs_perc >= 10:
9683
return f"{percentage:.1f}"
97-
if percentage_abs >= 1:
84+
if abs_perc >= 1:
9885
return f"{percentage:.2f}"
9986
return f"{percentage:.3f}"

0 commit comments

Comments
 (0)