plot_histogram.py
This file
Path
scripts/plot_histogram.py
40 lines · 1.1 KiB · Python
Last changed by
plot_histogram.py: add --type and --binssharkdpbefore adoptiond441eef
History of this fileTake it
Add a script to plot histogramssharkdpbefore adoptionef8a166
lines 1–6why, one step back1#!/usr/bin/python23"""This program shows `hyperfine` benchmark results as a histogram."""45import argparse6import jsonShow the full range in histogramsDavid Peterbefore adoptiona3f0278
line 7why, one step back7import numpy as npAdd a script to plot histogramssharkdpbefore adoptionef8a166
lines 8–12why, one step back8import 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")plot_histogram.py: add --type and --binssharkdpbefore adoptiond441eef
lines 13–16why, one step back13parser.add_argument("--bins", help="Number of bins (default: auto)")14parser.add_argument(15 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"16)Add a script to plot histogramssharkdpbefore adoptionef8a166
lines 17–24why, one step back17args = parser.parse_args()1819with open(args.file) as f:20 results = json.load(f)["results"]2122commands = [b["command"] for b in results]23all_times = [b["times"] for b in results]24Fix computation of min/max time rangesharkdpbefore adoptiondab1da9
lines 25–27why, one step back25t_min = np.min(list(map(np.min, all_times)))26t_max = np.max(list(map(np.max, all_times)))27plot_histogram.py: add --type and --binssharkdpbefore adoptiond441eef
lines 28–30why, one step back28bins = int(args.bins) if args.bins else "auto"29histtype = args.type if args.type else "bar"30Auto-format Python scriptssharkdpbefore adoption8fff864
line 31why, one step back31plt.hist(plot_histogram.py: add --type and --binssharkdpbefore adoptiond441eef
line 32why, one step back32 all_times, label=commands, bins=bins, histtype=histtype, range=(t_min, t_max),Auto-format Python scriptssharkdpbefore adoption8fff864
lines 33–34why, one step back33)34plt.legend(prop={"family": ["Source Code Pro", "Fira Mono", "Courier New"]})Add a script to plot histogramssharkdpbefore adoptionef8a166
lines 35–40why, one step back3536plt.xlabel("Time [s]")37if args.title:38 plt.title(args.title)3940plt.show()