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 fileTake it
The walk stopped after 200 commits: lines older than c44aa1d point at it.
Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d
line 1why, one step back1#!/usr/bin/env pythonAdd PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb
lines 2–7why, one step back2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "numpy",6# ]7# ///Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d
lines 8–10why, one step back89import argparse10import jsonAdd --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482
line 11why, one step back11from enum import EnumSort importsDavid Peterbefore adoptionbc37eff
line 12why, one step back12Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d
lines 13–14why, one step back13import numpy as np14Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482
lines 15–34why, one step back1516class 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"3334Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d
lines 35–36why, one step back35parser = 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 back37parser.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 back45args = parser.parse_args()46Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482
lines 47–49why, one step back47unit = Unit.MILLISECOND if args.unit == "millisecond" else Unit.SECOND48unit_str = str(unit)49Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d
lines 50–56why, one step back50with 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 back57 ts = [t * unit.factor() for t in ts]58Fix clippy suggestionsDavid Peterbefore adoptionc44aa1d
lines 59–65why, one step back59 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 - p2565Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
lines 66–67why, one step back66 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 back68 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 back73 print()74 print(" percentiles:")Add --time-unit option to 'advanced_statistics.py'David Peterbefore adoptionf78f482
lines 75–78why, one step back75 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 back79 print()