plot_histogram.py
This file
Path
scripts/plot_histogram.py
107 lines · 2.6 KiB · Python
Last changed by
Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
History of this fileTake it
The walk stopped after 200 commits: lines older than 9859eca point at it.
Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 1why, one step back1#!/usr/bin/env pythonAdd PEP-723 inline metadataDavid Peterbefore adoptioncfae9cb
lines 2–9why, one step back2# /// script3# requires-python = ">=3.10"4# dependencies = [5# "matplotlib",6# "pyqt6",7# "numpy",8# ]9# ///Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 10–14why, one step back1011"""This program shows `hyperfine` benchmark results as a histogram."""1213import argparse14import jsonSort importsDavid Peterbefore adoptionbc37eff
line 15why, one step back15Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
line 16why, one step back16import matplotlib.pyplot as pltSort importsDavid Peterbefore adoptionbc37eff
line 17why, one step back17import numpy as npAlways compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 18–26why, one step back1819parser = argparse.ArgumentParser(description=__doc__)20parser.add_argument("file", help="JSON file with benchmark results")21parser.add_argument("--title", help="Plot title")22parser.add_argument(23 "--labels", help="Comma-separated list of entries for the plot legend"24)25parser.add_argument("--bins", help="Number of bins (default: auto)")26parser.add_argument(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
lines 27–44why, one step back27 "--legend-location",28 help="Location of the legend on plot (default: upper center)",29 choices=[30 "upper center",31 "lower center",32 "right",33 "left",34 "best",35 "upper left",36 "upper right",37 "lower left",38 "lower right",39 "center left",40 "center right",41 "center",42 ],43 default="upper center",44)add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 45why, one step back45parser.add_argument(Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 46–68why, one step back46 "--type", help="Type of histogram (*bar*, barstacked, step, stepfilled)"47)48parser.add_argument("-o", "--output", help="Save image to the given filename.")49parser.add_argument(50 "--t-min", metavar="T", help="Minimum time to be displayed (seconds)"51)52parser.add_argument(53 "--t-max", metavar="T", help="Maximum time to be displayed (seconds)"54)55parser.add_argument(56 "--log-count",57 help="Use a logarithmic y-axis for the event count",58 action="store_true",59)6061args = parser.parse_args()6263with open(args.file) as f:64 results = json.load(f)["results"]6566if args.labels:67 labels = args.labels.split(",")68else:Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
lines 69–72why, one step back69 labels = [b.get("name", b["command"]) for b in results]70all_times = [71 [m["time_wall_clock"]["value"] for m in b["measurements"]] for b in results72]Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 73–79why, one step back7374t_min = float(args.t_min) if args.t_min else np.min(list(map(np.min, all_times)))75t_max = float(args.t_max) if args.t_max else np.max(list(map(np.max, all_times)))7677bins = int(args.bins) if args.bins else "auto"78histtype = args.type if args.type else "bar"79add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 80why, one step back80plt.figure(figsize=(10, 5))Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 81–87why, one step back81plt.hist(82 all_times,83 label=labels,84 bins=bins,85 histtype=histtype,86 range=(t_min, t_max),87)add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 88why, one step back88plt.legend(Update formatting, use f-stringsDavid Peterbefore adoption65dd84f
lines 89–93why, one step back89 loc=args.legend_location,90 fancybox=True,91 shadow=True,92 prop={"size": 10, "family": ["Source Code Pro", "Fira Mono", "Courier New"]},93)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 94–104why, one step back9495plt.xlabel("Time [s]")96if args.title:97 plt.title(args.title)9899if args.log_count:100 plt.yscale("log")101else:102 plt.ylim(0, None)103104if args.output:add: legend modification parameters and output DPI (#758)Spreadcatbefore adoptionef9e140
line 105why, one step back105 plt.savefig(args.output, dpi=600)Always compute relative speed comprison for export, closes #642David Peterbefore adoption9859eca
lines 106–107why, one step back106else:107 plt.show()