Skip to content
Myr

advanced_statistics.py

This file

Path

scripts/advanced_statistics.py

79 lines · 1.9 KiB · Python

Last changed by

Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482

History of this file

Take it

The walk stopped after 200 commits: lines older than c44aa1d point at it.

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

line 1why, one step back
1#!/usr/bin/env python

Add PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb

lines 2–7why, one step back
2# /// script
3# requires-python = ">=3.10"4# dependencies = [5# "numpy",6# ]7# ///

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 8–10why, one step back
8
9import argparse10import json

Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482

line 11why, one step back
11from enum import Enum

Sort importsDavid Peterbefore adoptionbc37eff

line 12why, one step back
12

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 13–14why, one step back
13import numpy as np
14

Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482

lines 15–34why, one step back
15
16class Unit(Enum):17 SECOND = 118 MILLISECOND = 21920 def factor(self):21 match self:22 case Unit.SECOND:23 return 124 case Unit.MILLISECOND:25 return 1e32627 def __str__(self):28 match self:29 case Unit.SECOND:30 return "s"31 case Unit.MILLISECOND:32 return "ms"3334

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 35–36why, one step back
35parser = argparse.ArgumentParser()
36parser.add_argument("file", help="JSON file with benchmark results")

Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482

lines 37–44why, one step back
37parser.add_argument(
38 "--time-unit",39 help="The unit of time.",40 default="second",41 action="store",42 choices=["second", "millisecond"],43 dest="unit",44)

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 45–46why, one step back
45args = parser.parse_args()
46

Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482

lines 47–49why, one step back
47unit = Unit.MILLISECOND if args.unit == "millisecond" else Unit.SECOND
48unit_str = str(unit)49

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 50–56why, one step back
50with open(args.file) as f:
51 results = json.load(f)["results"]5253commands = [b["command"] for b in results]54times = [b["times"] for b in results]5556for command, ts in zip(commands, times):

Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482

lines 57–58why, one step back
57 ts = [t * unit.factor() for t in ts]
58

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 59–65why, one step back
59 p05 = np.percentile(ts, 5)
60 p25 = np.percentile(ts, 25)61 p75 = np.percentile(ts, 75)62 p95 = np.percentile(ts, 95)6364 iqr = p75 - p2565

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

lines 66–67why, one step back
66 print(f"Command '{command}'")
67 print(f" runs: {len(ts):8d}")

Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482

lines 68–72why, one step back
68 print(f" mean: {np.mean(ts):8.3f} {unit_str}")
69 print(f" stddev: {np.std(ts, ddof=1):8.3f} {unit_str}")70 print(f" median: {np.median(ts):8.3f} {unit_str}")71 print(f" min: {np.min(ts):8.3f} {unit_str}")72 print(f" max: {np.max(ts):8.3f} {unit_str}")

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

lines 73–74why, one step back
73 print()
74 print(" percentiles:")

Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482

lines 75–78why, one step back
75 print(f" P_05 .. P_95: {p05:.3f} {unit_str} .. {p95:.3f} {unit_str}")
76 print(77 f" P_25 .. P_75: {p25:.3f} {unit_str} .. {p75:.3f} {unit_str} (IQR = {iqr:.3f} {unit_str})"78 )

Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d

line 79why, one step back
79 print()