Skip to content

Commit ab728f7

Browse files
authored
Merge pull request #1975 from codeflash-ai/codeflash/optimize-pr1941-2026-04-02T18.50.56
⚡️ Speed up function `validate_and_format_benchmark_table` by 45% in PR #1941 (`cf-compare-copy-benchmarks`)
2 parents 6965e98 + cfcbae5 commit ab728f7

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

codeflash/benchmarking/utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

3+
import logging
34
import shutil
5+
from operator import itemgetter
46
from typing import TYPE_CHECKING, Optional
57

68
from rich.console import Console
@@ -19,24 +21,27 @@ def validate_and_format_benchmark_table(
1921
function_benchmark_timings: dict[str, dict[BenchmarkKey, float]], total_benchmark_timings: dict[BenchmarkKey, float]
2022
) -> dict[str, list[tuple[BenchmarkKey, float, float, float]]]:
2123
function_to_result = {}
22-
# Process each function's benchmark data
24+
scale = 1_000_000.0
2325
for func_path, test_times in function_benchmark_timings.items():
2426
# Sort by percentage (highest first)
2527
sorted_tests = []
2628
for benchmark_key, func_time in test_times.items():
2729
total_time = total_benchmark_timings.get(benchmark_key, 0)
2830
if func_time > total_time:
29-
logger.debug(f"Skipping test {benchmark_key} due to func_time {func_time} > total_time {total_time}")
3031
# If the function time is greater than total time, likely to have multithreading / multiprocessing issues.
3132
# Do not try to project the optimization impact for this function.
33+
if logger.isEnabledFor(logging.DEBUG):
34+
logger.debug(
35+
f"Skipping test {benchmark_key} due to func_time {func_time} > total_time {total_time}"
36+
)
3237
sorted_tests.append((benchmark_key, 0.0, 0.0, 0.0))
3338
elif total_time > 0:
3439
percentage = (func_time / total_time) * 100
3540
# Convert nanoseconds to milliseconds
36-
func_time_ms = func_time / 1_000_000
37-
total_time_ms = total_time / 1_000_000
41+
func_time_ms = func_time / scale
42+
total_time_ms = total_time / scale
3843
sorted_tests.append((benchmark_key, total_time_ms, func_time_ms, percentage))
39-
sorted_tests.sort(key=lambda x: x[3], reverse=True)
44+
sorted_tests.sort(key=itemgetter(3), reverse=True)
4045
function_to_result[func_path] = sorted_tests
4146
return function_to_result
4247

0 commit comments

Comments
 (0)