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

1#!/usr/bin/env python2# /// script3# requires-python = ">=3.10"4# dependencies = [5#     "scipy",6# ]7# ///89"""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 sys1617from scipy import stats1819parser = 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)2930a, b = (x["command"] for x in results[:2])31X, Y = (x["times"] for x in results[:2])3233print(f"Command 1: {a}")34print(f"Command 2: {b}\n")3536t, p = stats.ttest_ind(X, Y, equal_var=False)37th = 0.0538dispose = p < th39print(f"t = {t:.3}, p = {p:.3}")40print()4142if dispose:43    print(f"There is a difference between the two benchmarks (p < {th}).")44else:45    print(f"The two benchmarks are almost the same (p >= {th}).")