| 1 |
""" |
| 2 |
# Plotting Benchmark Results |
| 3 |
|
| 4 |
## Usage |
| 5 |
|
| 6 |
usage: benchmark_plot.py [-h] [--test TEST] [--kind KIND] [--show] [--save] |
| 7 |
filename name |
| 8 |
|
| 9 |
Plotting |
| 10 |
|
| 11 |
positional arguments: |
| 12 |
filename path to file with benchmark data |
| 13 |
name type name |
| 14 |
|
| 15 |
optional arguments: |
| 16 |
-h, --help show this help message and exit |
| 17 |
--test TEST |
| 18 |
--kind KIND |
| 19 |
--show |
| 20 |
--save |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import print_function |
| 25 |
|
| 26 |
import argparse |
| 27 |
import matplotlib |
| 28 |
import matplotlib.pyplot as plt |
| 29 |
from collections import OrderedDict |
| 30 |
|
| 31 |
class TreeDict(OrderedDict): |
| 32 |
def __missing__(self, key): |
| 33 |
self[key] = value = TreeDict() |
| 34 |
return value |
| 35 |
|
| 36 |
def order_kinds(kinds): |
| 37 |
for idx, kind in enumerate(kinds): |
| 38 |
if kind.startswith('Sorted'): |
| 39 |
del kinds[idx] |
| 40 |
kinds.insert(0, kind) |
| 41 |
break |
| 42 |
return kinds |
| 43 |
|
| 44 |
def test_plot(test): |
| 45 |
ax = plt.gca() |
| 46 |
ax.grid(linestyle='dashed', linewidth=0.5) |
| 47 |
cmap = matplotlib.cm.get_cmap('Set1') |
| 48 |
colors = list(cmap.colors) |
| 49 |
colors[0], colors[1] = colors[1], colors[0] |
| 50 |
del colors[5] |
| 51 |
ax.set_prop_cycle('color', colors) |
| 52 |
kinds = args.kind or list(data[test]) |
| 53 |
for order, kind in enumerate(kinds): |
| 54 |
kind_plot(test, kind, len(kinds) - order) |
| 55 |
plt.ylim(ymin=9e-7) |
| 56 |
plt.loglog() |
| 57 |
plt.title(args.name + ' Performance: ' + test) |
| 58 |
plt.ylabel('Seconds') |
| 59 |
plt.xlabel('List Size') |
| 60 |
plt.legend(kinds, loc=2) |
| 61 |
|
| 62 |
def kind_plot(test, kind, zorder): |
| 63 |
sizes = sorted(data[test][kind].keys()) |
| 64 |
# Timer isn't any better than micro-second resolution. |
| 65 |
yvalues = [max(1e-6, data[test][kind][size][5]) for size in sizes] |
| 66 |
plt.plot(sizes, yvalues, marker='s', zorder=zorder) |
| 67 |
|
| 68 |
if __name__ == '__main__': |
| 69 |
parser = argparse.ArgumentParser(description='Plotting') |
| 70 |
parser.add_argument('filename', help='path to file with benchmark data') |
| 71 |
parser.add_argument('name', help='type name') |
| 72 |
parser.add_argument('--test', action='append', help='filter tests by name') |
| 73 |
parser.add_argument('--kind', action='append', help='filter types by name') |
| 74 |
parser.add_argument('--suffix', default='', help='suffix for output') |
| 75 |
parser.add_argument('--show', action='store_true', help='show results') |
| 76 |
parser.add_argument('--save', action='store_true', help='save results') |
| 77 |
args = parser.parse_args() |
| 78 |
|
| 79 |
text = open(args.filename).read() |
| 80 |
|
| 81 |
lines = text.splitlines() |
| 82 |
lines = [line.split() for line in lines] |
| 83 |
for line in lines: |
| 84 |
line[2] = int(line[2]) |
| 85 |
line[3:] = map(float, line[3:]) |
| 86 |
data = TreeDict() |
| 87 |
for line in lines: |
| 88 |
data[line[0]][line[1]][line[2]] = line |
| 89 |
|
| 90 |
tests = args.test or list(data) |
| 91 |
|
| 92 |
for test in tests: |
| 93 |
test_plot(test) |
| 94 |
|
| 95 |
if args.show: |
| 96 |
plt.show() |
| 97 |
|
| 98 |
if args.save: |
| 99 |
plt.savefig('{0}{1}-{2}.png'.format(args.name, args.suffix, test)) |
| 100 |
|
| 101 |
plt.close() |