Skip to content
Myr

welch_ttest.py

This file

Path

scripts/welch_ttest.py

45 lines · 1.0 KiB · Python

Last changed by

Sort importsDavid Peterbefore adoptionbc37eff

History of this file

Take it

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

Fix clippy warningsDavid Peterbefore adoptionec43213

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# "scipy",6# ]7# ///

Fix clippy warningsDavid Peterbefore adoptionec43213

lines 8–15why, one step back
8
9"""This script performs Welch's t-test on a JSON export file with two10benchmark results to test whether or not the two distributions are11the same."""1213import argparse14import json15import sys

Sort importsDavid Peterbefore adoptionbc37eff

line 16why, one step back
16

Fix clippy warningsDavid Peterbefore adoptionec43213

lines 17–29why, one step back
17from scipy import stats
1819parser = argparse.ArgumentParser(description=__doc__)20parser.add_argument("file", help="JSON file with two benchmark results")21args = parser.parse_args()2223with open(args.file) as f:24 results = json.load(f)["results"]2526if len(results) != 2:27 print("The input file has to contain exactly two benchmarks")28 sys.exit(1)29

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

lines 30–31why, one step back
30a, b = (x["command"] for x in results[:2])
31X, Y = (x["times"] for x in results[:2])

Fix clippy warningsDavid Peterbefore adoptionec43213

line 32why, one step back
32

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

lines 33–34why, one step back
33print(f"Command 1: {a}")
34print(f"Command 2: {b}\n")

Fix clippy warningsDavid Peterbefore adoptionec43213

lines 35–38why, one step back
35
36t, p = stats.ttest_ind(X, Y, equal_var=False)37th = 0.0538dispose = p < th

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

line 39why, one step back
39print(f"t = {t:.3}, p = {p:.3}")

Fix clippy warningsDavid Peterbefore adoptionec43213

lines 40–42why, one step back
40print()
4142if dispose:

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

line 43why, one step back
43 print(f"There is a difference between the two benchmarks (p < {th}).")

Fix clippy warningsDavid Peterbefore adoptionec43213

line 44why, one step back
44else:

Update formatting, use f-stringsDavid Peterbefore adoption65dd84f

line 45why, one step back
45 print(f"The two benchmarks are almost the same (p >= {th}).")