From 987e4a9c8cdf0567fd54a8820426365a88f156d5 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Wed, 3 Apr 2024 10:21:17 +0200 Subject: [PATCH] Improve benchmark accuracy and output. --- .github/workflows/ci.yml | 4 ++-- benchmark/telco_fractions.py | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 66815ba..e8c568e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -78,7 +78,7 @@ jobs: - name: Running benchmark run: | - python benchmark/telco_fractions.py + python benchmark/telco_fractions.py -n 250 python benchmark/microbench.py create pydigits Linux: @@ -201,7 +201,7 @@ jobs: - name: Running benchmark run: | - python benchmark/telco_fractions.py + python benchmark/telco_fractions.py -n 250 python benchmark/microbench.py create pydigits merge-wheels: diff --git a/benchmark/telco_fractions.py b/benchmark/telco_fractions.py index a27d4b5..3aa04b8 100644 --- a/benchmark/telco_fractions.py +++ b/benchmark/telco_fractions.py @@ -104,18 +104,20 @@ def run(cls): def main(n, cls=Fraction): for _ in range(5): run(cls) # warmup - times = [] - for _ in range(n): - times.append(run(cls)) + times = [run(cls) for _ in range(n)] return times +def percentile(values, percent): + return values[len(values) * percent // 100] + + if __name__ == "__main__": import optparse parser = optparse.OptionParser( usage="%prog [options]", description="Test the performance of the Telco fractions benchmark") - parser.add_option("-n", "--num_runs", action="store", type="int", default=16, + parser.add_option("-n", "--num_runs", action="store", type="int", default=200, dest="num_runs", help="Number of times to repeat the benchmark.") parser.add_option("--use-decimal", action="store_true", default=False, dest="use_decimal", help="Run benchmark with Decimal instead of Fraction.") @@ -130,10 +132,12 @@ def main(n, cls=Fraction): from fractions import Fraction as num_class results = main(options.num_runs, num_class) - for result in results: - print(result) + #for result in results: + # print(result) + #print() - print() results.sort() + print('%.4f (15%%)' % percentile(results, 15)) + print('%.4f (median)' % percentile(results, 50)) + print('%.4f (85%%)' % percentile(results, 85)) print('%.4f (mean)' % (fsum(results[1:-1]) / (len(results) - 2))) - print('%.4f (median)' % (results[len(results) // 2]))