|
| 1 | +"""Micro-benchmarks for mkl_umath unary ufuncs. |
| 2 | +
|
| 3 | +Times each ufunc over a Cartesian product of |
| 4 | + dtype in [float32, float64] |
| 5 | + size in [10_000, 100_000, 1_000_000] |
| 6 | +
|
| 7 | +Arrays are pre-allocated in setup() and reused across timing calls. |
| 8 | +Patching is applied once at package import via benchmarks._patch_setup. |
| 9 | +""" |
| 10 | + |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +_UFUNC_CONFIGS = { |
| 14 | + "exp": {"func": np.exp, "low": -10.0, "high": 10.0}, |
| 15 | + "exp2": {"func": np.exp2, "low": -10.0, "high": 10.0}, |
| 16 | + "expm1": {"func": np.expm1, "low": -10.0, "high": 10.0}, |
| 17 | + "log": {"func": np.log, "low": 1e-3, "high": 1e3}, |
| 18 | + "log2": {"func": np.log2, "low": 1e-3, "high": 1e3}, |
| 19 | + "log10": {"func": np.log10, "low": 1e-3, "high": 1e3}, |
| 20 | + "log1p": {"func": np.log1p, "low": 0.0, "high": 10.0}, |
| 21 | + "sin": {"func": np.sin, "low": -np.pi, "high": np.pi}, |
| 22 | + "cos": {"func": np.cos, "low": -np.pi, "high": np.pi}, |
| 23 | + "tan": {"func": np.tan, "low": -1.4, "high": 1.4}, |
| 24 | + "arcsin": {"func": np.arcsin, "low": -1.0, "high": 1.0}, |
| 25 | + "arccos": {"func": np.arccos, "low": -1.0, "high": 1.0}, |
| 26 | + "arctan": {"func": np.arctan, "low": -10.0, "high": 10.0}, |
| 27 | + "sinh": {"func": np.sinh, "low": -5.0, "high": 5.0}, |
| 28 | + "cosh": {"func": np.cosh, "low": -5.0, "high": 5.0}, |
| 29 | + "tanh": {"func": np.tanh, "low": -5.0, "high": 5.0}, |
| 30 | + "arcsinh": {"func": np.arcsinh, "low": -10.0, "high": 10.0}, |
| 31 | + "arccosh": {"func": np.arccosh, "low": 1.0, "high": 100.0}, |
| 32 | + "arctanh": {"func": np.arctanh, "low": -0.99, "high": 0.99}, |
| 33 | + "sqrt": {"func": np.sqrt, "low": 0.0, "high": 100.0}, |
| 34 | + "cbrt": {"func": np.cbrt, "low": -100.0, "high": 100.0}, |
| 35 | + "square": {"func": np.square, "low": -10.0, "high": 10.0}, |
| 36 | + "fabs": {"func": np.fabs, "low": -100.0, "high": 100.0}, |
| 37 | + "absolute": {"func": np.absolute, "low": -100.0, "high": 100.0}, |
| 38 | + "reciprocal": {"func": np.reciprocal, "low": 0.01, "high": 100.0}, |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +class BenchMicro: |
| 43 | + params = ( |
| 44 | + sorted(_UFUNC_CONFIGS.keys()), |
| 45 | + ["float32", "float64"], |
| 46 | + [10_000, 100_000, 1_000_000], |
| 47 | + ) |
| 48 | + param_names = ["ufunc", "dtype", "size"] |
| 49 | + |
| 50 | + def setup(self, ufunc, dtype, size): |
| 51 | + cfg = _UFUNC_CONFIGS[ufunc] |
| 52 | + rng = np.random.default_rng(42) |
| 53 | + self.x = rng.uniform(cfg["low"], cfg["high"], size).astype(dtype) |
| 54 | + self._func = cfg["func"] |
| 55 | + self._func(self.x) |
| 56 | + |
| 57 | + def time_micro(self, ufunc, dtype, size): |
| 58 | + self._func(self.x) |
| 59 | + |
| 60 | + |
| 61 | +class BenchArctan2: |
| 62 | + """Binary ufunc arctan2""" |
| 63 | + |
| 64 | + params = (["float32", "float64"], [10_000, 100_000, 1_000_000]) |
| 65 | + param_names = ["dtype", "size"] |
| 66 | + |
| 67 | + def setup(self, dtype, size): |
| 68 | + rng = np.random.default_rng(42) |
| 69 | + self.y = rng.uniform(-1.0, 1.0, size).astype(dtype) |
| 70 | + self.x = rng.uniform(-1.0, 1.0, size).astype(dtype) |
| 71 | + np.arctan2(self.y, self.x) |
| 72 | + |
| 73 | + def time_arctan2(self, dtype, size): |
| 74 | + np.arctan2(self.y, self.x) |
| 75 | + |
| 76 | + |
| 77 | +class BenchPower: |
| 78 | + """Binary ufunc power (arbitrary exponent via MKL vdPow)""" |
| 79 | + |
| 80 | + params = (["float32", "float64"], [10_000, 100_000, 1_000_000]) |
| 81 | + param_names = ["dtype", "size"] |
| 82 | + |
| 83 | + def setup(self, dtype, size): |
| 84 | + rng = np.random.default_rng(42) |
| 85 | + self.base = rng.uniform(0.1, 10.0, size).astype(dtype) |
| 86 | + self.exp = rng.uniform(0.5, 3.0, size).astype(dtype) |
| 87 | + np.power(self.base, self.exp) |
| 88 | + |
| 89 | + def time_power(self, dtype, size): |
| 90 | + np.power(self.base, self.exp) |
0 commit comments