summaryrefslogtreecommitdiff
path: root/benchmarks
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2019-05-28 10:09:13 -0700
committerGitHub <noreply@github.com>2019-05-28 10:09:13 -0700
commit22239d120f59826e8a2c758f4bee9893e835f511 (patch)
tree185c530dadfd28d5b47753de6f4985be61b8a1f2 /benchmarks
parent5b06588ec34d2b29502059a4fd86e24da8ddfc96 (diff)
parent70d6293bf8ae48e68844d34def56e9fb59027433 (diff)
downloadnumpy-22239d120f59826e8a2c758f4bee9893e835f511.tar.gz
Merge pull request #13163 from mattip/randomgen
ENH: randomgen This merges randomgen into numpy, which was originally developed at https://github.com/bashtage/randomgen and provides a new and improved API for random number generation with much new and improved functionality.
Diffstat (limited to 'benchmarks')
-rw-r--r--benchmarks/benchmarks/bench_random.py97
1 files changed, 93 insertions, 4 deletions
diff --git a/benchmarks/benchmarks/bench_random.py b/benchmarks/benchmarks/bench_random.py
index 9d84d83d3..c9c7baf7c 100644
--- a/benchmarks/benchmarks/bench_random.py
+++ b/benchmarks/benchmarks/bench_random.py
@@ -4,6 +4,7 @@ from .common import Benchmark
import numpy as np
+from numpy.random import RandomState, Generator
class Random(Benchmark):
params = ['normal', 'uniform', 'weibull 1', 'binomial 10 0.5',
@@ -69,14 +70,102 @@ class Randint_dtype(Benchmark):
class Permutation(Benchmark):
def setup(self):
self.n = 10000
- self.a_1d = np.random.random_sample(self.n)
- self.a_2d = np.random.random_sample((self.n, 2))
-
+ self.a_1d = np.random.random(self.n)
+ self.a_2d = np.random.random((self.n, 2))
+
def time_permutation_1d(self):
np.random.permutation(self.a_1d)
def time_permutation_2d(self):
- np.random.permutation(self.a_2d)
+ np.random.permutation(self.a_2d)
def time_permutation_int(self):
np.random.permutation(self.n)
+
+nom_size = 100000
+
+class RNG(Benchmark):
+ param_names = ['rng']
+ params = ['DSFMT', 'PCG64', 'PCG32', 'MT19937', 'Xoshiro256',
+ 'Xoshiro512', 'Philox', 'ThreeFry', 'numpy']
+
+ def setup(self, bitgen):
+ if bitgen == 'numpy':
+ self.rg = np.random.RandomState()
+ else:
+ self.rg = Generator(getattr(np.random, bitgen)())
+ self.rg.random()
+ self.int32info = np.iinfo(np.int32)
+ self.uint32info = np.iinfo(np.uint32)
+ self.uint64info = np.iinfo(np.uint64)
+
+ def time_raw(self, bitgen):
+ if bitgen == 'numpy':
+ self.rg.random_integers(self.int32info.max, size=nom_size)
+ else:
+ self.rg.integers(self.int32info.max, size=nom_size, endpoint=True)
+
+ def time_32bit(self, bitgen):
+ min, max = self.uint32info.min, self.uint32info.max
+ if bitgen == 'numpy':
+ self.rg.randint(min, max + 1, nom_size, dtype=np.uint32)
+ else:
+ self.rg.integers(min, max + 1, nom_size, dtype=np.uint32)
+
+ def time_64bit(self, bitgen):
+ min, max = self.uint64info.min, self.uint64info.max
+ if bitgen == 'numpy':
+ self.rg.randint(min, max + 1, nom_size, dtype=np.uint64)
+ else:
+ self.rg.integers(min, max + 1, nom_size, dtype=np.uint64)
+
+ def time_normal_zig(self, bitgen):
+ self.rg.standard_normal(nom_size)
+
+class Bounded(Benchmark):
+ u8 = np.uint8
+ u16 = np.uint16
+ u32 = np.uint32
+ u64 = np.uint64
+ param_names = ['rng', 'dt_max']
+ params = [['DSFMT', 'PCG64', 'PCG32', 'MT19937','Xoshiro256',
+ 'Xoshiro512', 'Philox', 'ThreeFry', 'numpy'],
+ [[u8, 95],
+ [u8, 64], # Worst case for legacy
+ [u8, 127], # Best case for legacy
+ [u16, 95],
+ [u16, 1024], # Worst case for legacy
+ [u16, 1535], # Typ. avg. case for legacy
+ [u16, 2047], # Best case for legacy
+ [u32, 1024], # Worst case for legacy
+ [u32, 1535], # Typ. avg. case for legacy
+ [u32, 2047], # Best case for legacy
+ [u64, 95],
+ [u64, 1024], # Worst case for legacy
+ [u64, 1535], # Typ. avg. case for legacy
+ [u64, 2047], # Best case for legacy
+ ]]
+
+ def setup(self, bitgen, args):
+ if bitgen == 'numpy':
+ self.rg = np.random.RandomState()
+ else:
+ self.rg = Generator(getattr(np.random, bitgen)())
+ self.rg.random()
+
+ def time_bounded(self, bitgen, args):
+ """
+ Timer for 8-bit bounded values.
+
+ Parameters (packed as args)
+ ----------
+ dt : {uint8, uint16, uint32, unit64}
+ output dtype
+ max : int
+ Upper bound for range. Lower is always 0. Must be <= 2**bits.
+ """
+ dt, max = args
+ if bitgen == 'numpy':
+ self.rg.randint(0, max + 1, nom_size, dtype=dt)
+ else:
+ self.rg.integers(0, max + 1, nom_size, dtype=dt)