MCPcopy Create free account
hub / github.com/codeflash-ai/codeflash / humanize_runtime

Function humanize_runtime

codeflash/code_utils/time_utils.py:7–48  ·  view source on GitHub ↗
(time_in_ns: int)

Source from the content-addressed store, hash-verified

5
6
7def humanize_runtime(time_in_ns: int) -> str:
8 runtime_human: str = str(time_in_ns)
9 units = "nanoseconds"
10 if 1 <= time_in_ns < 2:
11 units = "nanosecond"
12
13 if time_in_ns / 1000 >= 1:
14 time_micro = float(time_in_ns) / 1000
15 runtime_human = humanize.precisedelta(dt.timedelta(microseconds=time_micro), minimum_unit="microseconds")
16
17 units = re.split(r",|\s", runtime_human)[1]
18
19 if units == "microseconds" or units == "microsecond":
20 runtime_human = "%.3g" % time_micro
21 elif units == "milliseconds" or units == "millisecond":
22 runtime_human = "%.3g" % (time_micro / 1000)
23 elif units == "seconds" or units == "second":
24 runtime_human = "%.3g" % (time_micro / (1000**2))
25 elif units == "minutes" or units == "minute":
26 runtime_human = "%.3g" % (time_micro / (60 * 1000**2))
27 else: # hours
28 runtime_human = "%.3g" % (time_micro / (3600 * 1000**2))
29
30 runtime_human_parts = str(runtime_human).split(".")
31 if len(runtime_human_parts[0]) == 1:
32 if len(runtime_human_parts) == 1:
33 runtime_human = f"{runtime_human_parts[0]}.00"
34 elif len(runtime_human_parts[1]) >= 2:
35 runtime_human = f"{runtime_human_parts[0]}.{runtime_human_parts[1][0:2]}"
36 else:
37 runtime_human = (
38 f"{runtime_human_parts[0]}.{runtime_human_parts[1]}{'0' * (2 - len(runtime_human_parts[1]))}"
39 )
40 elif len(runtime_human_parts[0]) == 2:
41 if len(runtime_human_parts) > 1:
42 runtime_human = f"{runtime_human_parts[0]}.{runtime_human_parts[1][0]}"
43 else:
44 runtime_human = f"{runtime_human_parts[0]}.0"
45 else:
46 runtime_human = runtime_human_parts[0]
47
48 return f"{runtime_human} {units}"

Callers 5

test_humanize_runtimeFunction · 0.90
to_console_stringMethod · 0.90
to_jsonMethod · 0.90

Calls

no outgoing calls

Tested by 1

test_humanize_runtimeFunction · 0.72