|
| 1 | +# Copyright (c) 2026, Intel Corporation |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions are met: |
| 5 | +# |
| 6 | +# * Redistributions of source code must retain the above copyright notice, |
| 7 | +# this list of conditions and the following disclaimer. |
| 8 | +# * Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# * Neither the name of Intel Corporation nor the names of its contributors |
| 12 | +# may be used to endorse or promote products derived from this software |
| 13 | +# without specific prior written permission. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 19 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + |
| 26 | +"""Benchmarks for array-valued distribution parameters. |
| 27 | +
|
| 28 | +One parameter value per output element takes a separate code path from |
| 29 | +scalar parameters, so these are tracked apart from bench_continuous.py and |
| 30 | +bench_discrete.py. |
| 31 | +""" |
| 32 | + |
| 33 | +import numpy as np |
| 34 | + |
| 35 | +from ._utils import _SEED, _make_state |
| 36 | + |
| 37 | +# Smaller than _utils._SIZES: the per-element path is much slower than a |
| 38 | +# scalar fill. |
| 39 | +_ARRAY_SIZES = [1_000, 100_000] |
| 40 | + |
| 41 | + |
| 42 | +class ArrayParams: |
| 43 | + """Distributions called with one parameter value per output element.""" |
| 44 | + |
| 45 | + params = [_ARRAY_SIZES] |
| 46 | + param_names = ["size"] |
| 47 | + |
| 48 | + def setup(self, size): |
| 49 | + self.rs = _make_state() |
| 50 | + rng = np.random.default_rng(_SEED) |
| 51 | + self.loc = rng.standard_normal(size) |
| 52 | + self.scale = rng.uniform(0.5, 2.0, size) |
| 53 | + self.high = self.loc + self.scale |
| 54 | + self.shape = rng.uniform(0.5, 5.0, size) |
| 55 | + self.lam = rng.uniform(1.0, 100.0, size) |
| 56 | + self.n = rng.integers(1, 100, size) |
| 57 | + self.p = rng.uniform(0.1, 0.9, size) |
| 58 | + self.rs.normal(self.loc, self.scale) |
| 59 | + self.rs.uniform(self.loc, self.high) |
| 60 | + self.rs.exponential(self.scale) |
| 61 | + self.rs.standard_gamma(self.shape) |
| 62 | + self.rs.poisson(self.lam) |
| 63 | + self.rs.binomial(self.n, self.p) |
| 64 | + |
| 65 | + def time_normal(self, size): |
| 66 | + self.rs.normal(self.loc, self.scale) |
| 67 | + |
| 68 | + def time_uniform(self, size): |
| 69 | + self.rs.uniform(self.loc, self.high) |
| 70 | + |
| 71 | + def time_exponential(self, size): |
| 72 | + self.rs.exponential(self.scale) |
| 73 | + |
| 74 | + def time_standard_gamma(self, size): |
| 75 | + self.rs.standard_gamma(self.shape) |
| 76 | + |
| 77 | + def time_poisson(self, size): |
| 78 | + self.rs.poisson(self.lam) |
| 79 | + |
| 80 | + def time_binomial(self, size): |
| 81 | + self.rs.binomial(self.n, self.p) |
0 commit comments