Skip to content

Commit

Permalink
Add --time-unit option to 'advanced_statistics.py'
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Dec 1, 2024
1 parent 4e1e9f2 commit f78f482
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions scripts/advanced_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,54 @@

import argparse
import json
from enum import Enum

import numpy as np


class Unit(Enum):
SECOND = 1
MILLISECOND = 2

def factor(self):
match self:
case Unit.SECOND:
return 1
case Unit.MILLISECOND:
return 1e3

def __str__(self):
match self:
case Unit.SECOND:
return "s"
case Unit.MILLISECOND:
return "ms"


parser = argparse.ArgumentParser()
parser.add_argument("file", help="JSON file with benchmark results")
parser.add_argument(
"--time-unit",
help="The unit of time.",
default="second",
action="store",
choices=["second", "millisecond"],
dest="unit",
)
args = parser.parse_args()

unit = Unit.MILLISECOND if args.unit == "millisecond" else Unit.SECOND
unit_str = str(unit)

with open(args.file) as f:
results = json.load(f)["results"]

commands = [b["command"] for b in results]
times = [b["times"] for b in results]

for command, ts in zip(commands, times):
ts = [t * unit.factor() for t in ts]

p05 = np.percentile(ts, 5)
p25 = np.percentile(ts, 25)
p75 = np.percentile(ts, 75)
Expand All @@ -31,13 +65,15 @@

print(f"Command '{command}'")
print(f" runs: {len(ts):8d}")
print(f" mean: {np.mean(ts):8.3f} s")
print(f" stddev: {np.std(ts, ddof=1):8.3f} s")
print(f" median: {np.median(ts):8.3f} s")
print(f" min: {np.min(ts):8.3f} s")
print(f" max: {np.max(ts):8.3f} s")
print(f" mean: {np.mean(ts):8.3f} {unit_str}")
print(f" stddev: {np.std(ts, ddof=1):8.3f} {unit_str}")
print(f" median: {np.median(ts):8.3f} {unit_str}")
print(f" min: {np.min(ts):8.3f} {unit_str}")
print(f" max: {np.max(ts):8.3f} {unit_str}")
print()
print(" percentiles:")
print(f" P_05 .. P_95: {p05:.3f} s .. {p95:.3f} s")
print(f" P_25 .. P_75: {p25:.3f} s .. {p75:.3f} s (IQR = {iqr:.3f} s)")
print(f" P_05 .. P_95: {p05:.3f} {unit_str} .. {p95:.3f} {unit_str}")
print(
f" P_25 .. P_75: {p25:.3f} {unit_str} .. {p75:.3f} {unit_str} (IQR = {iqr:.3f} {unit_str})"
)
print()

0 comments on commit f78f482

Please sign in to comment.