Skip to content
Myr

plot_histogram.py

This file

Path

scripts/plot_histogram.py

71 lines · 1.9 KiB · Python

Last changed by

Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c

History of this file

Take it

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

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 1–19why, one step back
1#!/usr/bin/env python
23"""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)

Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c

line 20why, one step back
20parser.add_argument("-o", "--output", help="Save image to the given filename.")

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 21–26why, one step back
21parser.add_argument(
22 "--t-min", metavar="T", help="Minimum time to be displayed (seconds)"23)24parser.add_argument(25 "--t-max", metavar="T", help="Maximum time to be displayed (seconds)"26)

Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c

lines 27–31why, one step back
27parser.add_argument(
28 "--log-count",29 help="Use a logarithmic y-axis for the event count",30 action="store_true",31)

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 32–50why, one step back
32
33args = parser.parse_args()3435with open(args.file) as f:36 results = json.load(f)["results"]3738if args.labels:39 labels = args.labels.split(",")40else:41 labels = [b["command"] for b in results]42all_times = [b["times"] for b in results]4344t_min = float(args.t_min) if args.t_min else np.min(list(map(np.min, all_times)))45t_max = float(args.t_max) if args.t_max else np.max(list(map(np.max, all_times)))4647bins = int(args.bins) if args.bins else "auto"48histtype = args.type if args.type else "bar"4950plt.hist(

Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c

lines 51–55why, one step back
51 all_times,
52 label=labels,53 bins=bins,54 histtype=histtype,55 range=(t_min, t_max),

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 56–61why, one step back
56)
57plt.legend(prop={"family": ["Source Code Pro", "Fira Mono", "Courier New"]})5859plt.xlabel("Time [s]")60if args.title:61 plt.title(args.title)

Add --log-count option to plot_histogram.pyDavid Peterbefore adoption77e7b6c

lines 62–66why, one step back
62
63if args.log_count:64 plt.yscale("log")65else:66 plt.ylim(0, None)

Refactor Markdown with Markup by using generic table emitterPhilipp Paulweberbefore adoption23d9fe2

lines 67–71why, one step back
67
68if args.output:69 plt.savefig(args.output)70else:71 plt.show()