Computes the difference between a reference value and a comparison value. Returns a string formatted as a signed percentage along with the absolute difference in milliseconds, e.g.: "+12.34% (+5.67 ms)". A color gradient is applied so that: - For positive differences, the tex
(ref_val, comp_val)
| 73 | return data |
| 74 | |
| 75 | def format_diff(ref_val, comp_val): |
| 76 | """ |
| 77 | Computes the difference between a reference value and a comparison value. |
| 78 | Returns a string formatted as a signed percentage along with the absolute difference |
| 79 | in milliseconds, e.g.: "+12.34% (+5.67 ms)". |
| 80 | |
| 81 | A color gradient is applied so that: |
| 82 | - For positive differences, the text is tinted red, with brighter red for +100% and above. |
| 83 | - For negative differences, the text is tinted green, with brighter green for -100% and below. |
| 84 | - At 0% the text appears white. |
| 85 | |
| 86 | If the reference value is zero, returns "N/A". |
| 87 | """ |
| 88 | if ref_val == 0: |
| 89 | return "N/A" |
| 90 | diff = comp_val - ref_val |
| 91 | perc_diff = diff / ref_val * 100.0 |
| 92 | |
| 93 | # Determine the color gradient. |
| 94 | if perc_diff >= 0: |
| 95 | # Clamp percentage to 100 if above 100. |
| 96 | p = min(perc_diff, 100) |
| 97 | R = 255 |
| 98 | # Green and Blue go from 255 at 0% to 0 at 100% |
| 99 | GB = int(255 - (255 * p / 100)) |
| 100 | G = GB |
| 101 | B = GB |
| 102 | else: |
| 103 | p = min(abs(perc_diff), 100) |
| 104 | G = 255 |
| 105 | # Red and Blue go from 255 at 0% to 0 at -100% |
| 106 | RB = int(255 - (255 * p / 100)) |
| 107 | R = RB |
| 108 | B = RB |
| 109 | |
| 110 | # ANSI 24-bit color escape sequence. |
| 111 | color_code = f"\033[38;2;{R};{G};{B}m" |
| 112 | reset_code = "\033[0m" |
| 113 | diff_str = f"{perc_diff:+.2f}% ({diff:+.2f} ms)" |
| 114 | return f"{color_code}{diff_str}{reset_code}" |
| 115 | |
| 116 | def main(reference_csv, comparison_csv): |
| 117 | # Read overall timing data from both CSV files. |