plot_histogram.py
This file
Path
scripts/plot_histogram.py
53 lines · 1.4 KiB · Python
Last changed by
Change parameter name in plotting scriptsBenjamin Rosemannbefore adoption49fcc4b
History of this fileTake it
1#!/usr/bin/env python23"""This program shows `hyperfine` benchmark results as a histogram."""45import argparse6import json7import numpy as np8import matplotlib.pyplot as plt910parser = argparse.ArgumentParser(description=__doc__)11parser.add_argument("file", help="JSON file with benchmark results")12parser.add_argument("--title", help="Plot title")13parser.add_argument(14 "--labels", help="Comma-separated list of entries for the plot legend"15)16parser.add_argument("--bins", help="Number of bins (default: auto)")17parser.add_argument(18 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"19)20parser.add_argument(21 "-o", "--output", help="Save image to the given filename."22)2324args = parser.parse_args()2526with open(args.file) as f:27 results = json.load(f)["results"]2829if args.labels:30 labels = args.labels.split(",")31else:32 labels = [b["command"] for b in results]33all_times = [b["times"] for b in results]3435t_min = np.min(list(map(np.min, all_times)))36t_max = np.max(list(map(np.max, all_times)))3738bins = int(args.bins) if args.bins else "auto"39histtype = args.type if args.type else "bar"4041plt.hist(42 all_times, label=labels, bins=bins, histtype=histtype, range=(t_min, t_max),43)44plt.legend(prop={"family": ["Source Code Pro", "Fira Mono", "Courier New"]})4546plt.xlabel("Time [s]")47if args.title:48 plt.title(args.title)4950if args.output:51 plt.savefig(args.output)52else:53 plt.show()