iteration_performance.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. # Copyright Hans Dembinski 2018 - 2019.
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE_1_0.txt or copy at
  5. # https://www.boost.org/LICENSE_1_0.txt)
  6. import numpy as np
  7. import json
  8. from collections import defaultdict
  9. import os
  10. import re
  11. import sys
  12. import matplotlib.pyplot as plt
  13. import matplotlib as mpl
  14. mpl.rcParams.update(mpl.rcParamsDefault)
  15. bench = defaultdict(lambda:[])
  16. data = json.load(open(sys.argv[1]))
  17. for benchmark in data["benchmarks"]:
  18. # Naive/(tuple, 3, inner)/4 3.44 ns
  19. m = re.match("(\S+)/\((\S+), (\d), (\S+)\)/(\d+)", benchmark["name"])
  20. name = m.group(1)
  21. hist = m.group(2)
  22. dim = int(m.group(3))
  23. cov = m.group(4)
  24. nbins = int(m.group(5))
  25. time = benchmark["cpu_time"]
  26. bench[(name, hist, dim, cov)].append((int(nbins) ** dim, time))
  27. fig, ax = plt.subplots(1, 3, figsize=(10, 5), sharex=True, sharey=True)
  28. if os.path.exists("/proc/cpuinfo"):
  29. cpuinfo = open("/proc/cpuinfo").read()
  30. m = re.search("model name\s*:\s*(.+)\n", cpuinfo)
  31. if m:
  32. plt.suptitle(m.group(1))
  33. plt.subplots_adjust(bottom=0.18, wspace=0, top=0.85, right=0.98, left=0.07)
  34. for iaxis, axis_type in enumerate(("tuple", "vector", "vector_of_variant")):
  35. plt.sca(ax[iaxis])
  36. plt.title(axis_type.replace("_", " "), y=1.02)
  37. handles = []
  38. for (name, axis_t, dim, cov), v in bench.items():
  39. if axis_t != axis_type: continue
  40. if cov != "inner": continue
  41. v = np.sort(v, axis=0).T
  42. # if "semi_dynamic" in axis: continue
  43. name2, col, ls = {
  44. "Naive": ("nested for", "0.5", ":"),
  45. "Indexed": ("indexed", "r", "-")}.get(name, (name, "k", "-"))
  46. h = plt.plot(v[0], v[1] / v[0], color=col, ls=ls, lw=dim,
  47. label=r"%s: $D=%i$" % (name2, dim))[0]
  48. handles.append(h)
  49. handles.sort(key=lambda x: x.get_label())
  50. plt.loglog()
  51. plt.sca(ax[0])
  52. plt.ylabel("CPU time in ns per bin")
  53. plt.legend(handles=handles, fontsize="x-small", frameon=False, handlelength=4, ncol=2)
  54. plt.figtext(0.5, 0.05, "number of bins", ha="center")
  55. plt.show()