1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#!/usr/bin/env python
from __future__ import annotations
import math
import multiprocessing
from timeit import timeit
print("Welcome to boost-histogram's performance report")
setup_1d = """
import numpy as np
import boost_histogram as bh
bins=100
ranges=(-1,1)
bins = np.asarray(bins).astype(np.int64)
ranges = np.asarray(ranges).astype(np.float64)
edges = np.linspace(*ranges, bins+1)
np.random.seed(42)
vals = np.random.normal(size=[50_000_000]).astype(np.float32)
"""
setup_2d = """
import numpy as np
import boost_histogram as bh
bins=(100, 100)
ranges=((-1,1),(-1,1))
bins = np.asarray(bins).astype(np.int64)
ranges = np.asarray(ranges).astype(np.float64)
edges = (np.linspace(*ranges[0,:], bins[0]+1),
np.linspace(*ranges[1,:], bins[1]+1))
np.random.seed(42)
vals = np.random.normal(size=[2, 5_000_000]).astype(np.float64)
"""
def timer(setup, statement, n=10):
t = timeit(statement, setup, number=n)
return (t * 1000) / n
def print_timer(setup, statement, name, storage, fill, flow, base=None, n=10):
time = timer(
setup, statement.format(name=name, storage=storage, fill=fill, flow=flow), n
)
speedup = 1 if base is None else base / time
print(
table_line.format(
name=name,
storage=storage,
fill=fill,
flow=str(flow),
time=time,
speedup=speedup,
)
)
return time
c = multiprocessing.cpu_count()
counts = [c // 2**x for x in reversed(range(int(math.log2(c) + 1)))]
table_header = "| Type | Storage | Threads | Flow | Time | Speedup |"
table_spacer = "|-------|---------------|----------|------|-----------|---------|"
table_line = (
"|{name:^7}|{storage:^15}|{fill:^9}|{flow:<6}|{time:>7.4g} ms |{speedup:>7.2g}x |"
)
print()
print("### 1D, 10 runs each")
print()
print(table_header)
print(table_spacer)
base = print_timer(
setup_1d,
"h, _ = np.histogram(vals, bins=bins, range=ranges)",
name="NumPy",
storage="uint64",
fill="",
flow=False,
)
print_timer(
setup_1d,
"hist = bh.Histogram(bh.axis.{name}(bins, *ranges, underflow={flow}, overflow={flow}), storage=bh.storage.{storage}()); hist.fill(vals)",
name="Regular",
storage="Int64",
fill="",
flow=False,
base=base,
)
print_timer(
setup_1d,
"hist = bh.Histogram(bh.axis.{name}(bins, *ranges, underflow={flow}, overflow={flow}), storage=bh.storage.{storage}()); hist.fill(vals)",
name="Regular",
storage="Int64",
fill="",
flow=True,
base=base,
)
for fill in counts:
print_timer(
setup_1d,
"hist = bh.Histogram(bh.axis.{name}(bins, *ranges, underflow={flow}, overflow={flow}), storage=bh.storage.{storage}()); hist.fill(vals, threads={fill})",
name="Regular",
storage="Int64",
fill=fill,
flow=True,
base=base,
)
for fill in counts:
print_timer(
setup_1d,
"hist = bh.Histogram(bh.axis.{name}(bins, *ranges, underflow={flow}, overflow={flow}), storage=bh.storage.{storage}()); hist.fill(vals, threads={fill})",
name="Regular",
storage="AtomicInt64",
fill=fill,
flow=True,
base=base,
)
print()
print("### 2D, 10 runs each")
print()
print(table_header)
print(table_spacer)
base = print_timer(
setup_2d,
"H, *ledges = np.histogram2d(*vals, bins=bins, range=ranges)",
name="NumPy",
storage="uint64",
fill="",
flow=False,
)
print_timer(
setup_2d,
"hist = bh.Histogram(bh.axis.{name}(bins[1], *ranges[0], underflow={flow}, overflow={flow}), bh.axis.{name}(bins[1], *ranges[1], underflow={flow}, overflow={flow}), storage=bh.storage.{storage}()); hist.fill(*vals)",
name="Regular",
storage="Int64",
fill="",
flow=False,
base=base,
)
for fill in counts:
print_timer(
setup_2d,
"hist = bh.Histogram(bh.axis.{name}(bins[1], *ranges[0], underflow={flow}, overflow={flow}), bh.axis.{name}(bins[1], *ranges[1], underflow={flow}, overflow={flow}), storage=bh.storage.{storage}()); hist.fill(*vals, threads={fill})",
name="Regular",
storage="Int64",
fill=fill,
flow=False,
base=base,
)
for fill in counts:
print_timer(
setup_2d,
"hist = bh.Histogram(bh.axis.{name}(bins[1], *ranges[0], underflow={flow}, overflow={flow}), bh.axis.{name}(bins[1], *ranges[1], underflow={flow}, overflow={flow}), storage=bh.storage.{storage}()); hist.fill(*vals, threads={fill})",
name="Regular",
storage="AtomicInt64",
fill=fill,
flow=False,
base=base,
)
|