Skip to content

Commit

Permalink
gas_diff_stats.py: Improve table and number formatting
Browse files Browse the repository at this point in the history
- Use blanks if percentage is infinite instead of misleadingly showing zero.
- Round percentages to nearest integer (but indicate when numbers are only close to zero rather than exactly zero). Differences below one percent are rarely relevant, if ever.
- Include % after the numbers.
- Put file names in backticks to get them displayed using fixed-width font on github.
  • Loading branch information
cameel committed Feb 21, 2024
1 parent 65031d3 commit 43274fd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions scripts/gas_diff_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,16 @@ def parse_git_diff(fname):
return collect_statistics(diff_output)

def stat(old, new):
return ((new - old) / old) * 100 if old else 0
if old == 0:
return ''
percentage = (new - old) / old * 100
prefix = (
# Distinguish actual zero from very small differences
'+' if round(percentage) == 0 and percentage > 0 else
'-' if round(percentage) == 0 and percentage < 0 else
''
)
return f'{prefix}{round(percentage)}%'

table = []

Expand All @@ -129,12 +138,12 @@ def stat(old, new):
ir_optimized = stat(parsed[0], parsed[3])
legacy_optimized = stat(parsed[1], parsed[4])
legacy = stat(parsed[2], parsed[5])
fname = fname.split('/', 3)[-1]
table += [map(str, [fname, ir_optimized, legacy_optimized, legacy])]
fname = f"`{fname.split('/', 3)[-1]}`"
table += [[fname, ir_optimized, legacy_optimized, legacy]]

if table:
print("<details><summary>Click for a table of gas differences</summary>\n")
table_header = ["File name", "IR-optimized (%)", "Legacy-Optimized (%)", "Legacy (%)"]
table_header = ["File name", "IR optimized", "Legacy optimized", "Legacy"]
print(tabulate(table, headers=table_header, tablefmt="github"))
print("</details>")
else:
Expand Down

0 comments on commit 43274fd

Please sign in to comment.