-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlatex_table.py
More file actions
executable file
·160 lines (129 loc) · 4.8 KB
/
latex_table.py
File metadata and controls
executable file
·160 lines (129 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
"""
Convert benchmark output to LaTeX tables.
This script parses benchmark output data and generates a formatted LaTeX table
with performance metrics (ns/float, instructions/float, instructions/cycle).
It formats numbers to two significant digits for readability.
"""
import sys
import re
import argparse
def format_to_two_sig_digits(value):
"""
Format a number to two significant digits with appropriate notation.
Args:
value: The number to format
Returns:
A string representation of the number with two significant digits
"""
if not isinstance(value, (int, float)) or value == 0:
return "N/A"
# Handle negative numbers
is_negative = value < 0
abs_value = abs(value)
exponent = 0
while abs_value >= 100:
abs_value /= 10
exponent += 1
while abs_value < 10:
abs_value *= 10
exponent -= 1
# Round to two significant digits
abs_value = round(abs_value, 0)
# Format the number
if exponent >= 0 and exponent <= 4:
format = f"{'-' if is_negative else ''}{abs_value * 10 ** exponent}"
format = format.replace(".0", "")
return format
elif exponent < 0 and exponent >= -4:
return f"{'-' if is_negative else ''}{abs_value * 10 ** exponent:.1f}"
else:
return f"{'-' if is_negative else ''}{abs_value:.1f}e{exponent}"
def parse_input(data):
"""
Parse benchmark output data to extract performance metrics.
Args:
data: Raw benchmark output as a string
Returns:
List of dictionaries containing parsed metrics for each algorithm
"""
lines = data.splitlines()
parsed_data = []
current_entry = None
for line in lines:
line = line.strip()
# Skip empty lines or comments
if not line or line.startswith("#"):
continue
# Match lines that start a new entry
# e.g., "just_string : 1365.92 MB/s ..."
match_entry = re.match(r"(\S+)\s*:\s*[\d.]+\s*MB/s", line)
if match_entry:
current_entry = {"name": match_entry.group(1)}
parsed_data.append(current_entry)
if not current_entry:
continue
# Match lines with ns/f
match_ns = re.search(r"([\d.]+)\s*ns/f", line)
if match_ns and current_entry:
current_entry["ns_per_float"] = float(match_ns.group(1))
# Match lines with instructions/float (i/f)
match_inst_float = re.search(r"([\d.]+)\s*i/f", line)
if match_inst_float and current_entry:
current_entry["inst_per_float"] = float(match_inst_float.group(1))
# Match lines with instructions/cycle (i/c)
match_inst_cycle = re.search(r"([\d.]+)\s*i/c", line)
if match_inst_cycle and current_entry:
current_entry["inst_per_cycle"] = float(match_inst_cycle.group(1))
# Filter out incomplete entries
return parsed_data
def generate_latex_table(raw_input):
"""
Generate a LaTeX table from benchmark output data.
Args:
raw_input: Raw benchmark output as a string
Returns:
Formatted LaTeX table as a string
"""
data = parse_input(raw_input)
latex_table = r"""
\begin{tabular}{lccc}
\toprule
\textbf{Name} & \textbf{ns/f} & \textbf{instructions/float} & \textbf{instructions/cycle} \\
\midrule
"""
for entry in data:
name = entry["name"].replace("_", "\\_") # Escape underscore for LaTeX
ns_per_float = format_to_two_sig_digits(entry['ns_per_float']) if 'ns_per_float' in entry else 'N/A'
inst_per_float = format_to_two_sig_digits(entry['inst_per_float']) if 'inst_per_float' in entry else 'N/A'
inst_per_cycle = format_to_two_sig_digits(entry['inst_per_cycle']) if 'inst_per_cycle' in entry else 'N/A'
latex_table += f"{name} & {ns_per_float} & {inst_per_float} & {inst_per_cycle} \\\\ \n"
latex_table += r"""\bottomrule
\end{tabular}
"""
return latex_table
def main():
"""Parse command line arguments and generate LaTeX table."""
parser = argparse.ArgumentParser(
description="Generate LaTeX table from performance data")
parser.add_argument(
"file", nargs="?",
help="Optional input file name (if not provided, reads from stdin)")
args = parser.parse_args()
# Read input data
if args.file:
try:
with open(args.file, "r") as f:
raw_input = f.read()
except FileNotFoundError:
print(f"Error: File '{args.file}' not found.", file=sys.stderr)
sys.exit(1)
except IOError as e:
print(f"Error reading file '{args.file}': {e}", file=sys.stderr)
sys.exit(1)
else:
raw_input = sys.stdin.read()
latex_output = generate_latex_table(raw_input)
print(latex_output)
if __name__ == "__main__":
main()