Skip to content

Commit 987e4a9

Browse files
committed
Improve benchmark accuracy and output.
1 parent 572ba5f commit 987e4a9

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878

7979
- name: Running benchmark
8080
run: |
81-
python benchmark/telco_fractions.py
81+
python benchmark/telco_fractions.py -n 250
8282
python benchmark/microbench.py create pydigits
8383
8484
Linux:
@@ -201,7 +201,7 @@ jobs:
201201

202202
- name: Running benchmark
203203
run: |
204-
python benchmark/telco_fractions.py
204+
python benchmark/telco_fractions.py -n 250
205205
python benchmark/microbench.py create pydigits
206206
207207
merge-wheels:

benchmark/telco_fractions.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,20 @@ def run(cls):
104104
def main(n, cls=Fraction):
105105
for _ in range(5):
106106
run(cls) # warmup
107-
times = []
108-
for _ in range(n):
109-
times.append(run(cls))
107+
times = [run(cls) for _ in range(n)]
110108
return times
111109

112110

111+
def percentile(values, percent):
112+
return values[len(values) * percent // 100]
113+
114+
113115
if __name__ == "__main__":
114116
import optparse
115117
parser = optparse.OptionParser(
116118
usage="%prog [options]",
117119
description="Test the performance of the Telco fractions benchmark")
118-
parser.add_option("-n", "--num_runs", action="store", type="int", default=16,
120+
parser.add_option("-n", "--num_runs", action="store", type="int", default=200,
119121
dest="num_runs", help="Number of times to repeat the benchmark.")
120122
parser.add_option("--use-decimal", action="store_true", default=False,
121123
dest="use_decimal", help="Run benchmark with Decimal instead of Fraction.")
@@ -130,10 +132,12 @@ def main(n, cls=Fraction):
130132
from fractions import Fraction as num_class
131133

132134
results = main(options.num_runs, num_class)
133-
for result in results:
134-
print(result)
135+
#for result in results:
136+
# print(result)
137+
#print()
135138

136-
print()
137139
results.sort()
140+
print('%.4f (15%%)' % percentile(results, 15))
141+
print('%.4f (median)' % percentile(results, 50))
142+
print('%.4f (85%%)' % percentile(results, 85))
138143
print('%.4f (mean)' % (fsum(results[1:-1]) / (len(results) - 2)))
139-
print('%.4f (median)' % (results[len(results) // 2]))

0 commit comments

Comments
 (0)