Skip to content

Commit d34aefa

Browse files
committed
ENH: Add outputless mode for raw
Add outputles mode for raw to allow accurate performance testing of core PRNG
1 parent 50d358e commit d34aefa

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

randomstate/performance.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ def run_timer(dist, command, numpy_command=None, setup='', random_type=''):
4141

4242
res = {}
4343
for rng in RNGS:
44-
print(str(rng))
4544
mod = 'randomstate.prng' if rng != 'random' else 'numpy'
4645
key = '-'.join((mod, rng, dist)).replace('"', '')
4746
command = numpy_command if 'numpy' in mod else command
@@ -67,6 +66,15 @@ def run_timer(dist, command, numpy_command=None, setup='', random_type=''):
6766
print('-' * 80)
6867

6968

69+
def timer_raw():
70+
dist = 'random_raw'
71+
command = 'rs.random_raw(size=1000000, output=False)'
72+
info = np.iinfo(np.int32)
73+
command_numpy = 'rs.random_integers({max},size=1000000)'
74+
command_numpy=command_numpy.format(max=info.max)
75+
run_timer(dist, command, command_numpy, SETUP, 'Standard normals (Ziggurat)')
76+
77+
7078
def timer_uniform():
7179
dist = 'random_sample'
7280
command = 'rs.random_sample(1000000)'
@@ -108,6 +116,7 @@ def timer_normal_zig():
108116

109117

110118
if __name__ == '__main__':
119+
timer_raw()
111120
timer_uniform()
112121
timer_32bit()
113122
timer_64bit()

randomstate/randomstate.pyx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ cdef class RandomState:
723723

724724
return array
725725

726-
def random_raw(self, size=None):
726+
def random_raw(self, size=None, output=True):
727727
"""
728728
random_raw(self, size=None)
729729
@@ -735,6 +735,9 @@ cdef class RandomState:
735735
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
736736
``m * n * k`` samples are drawn. Default is None, in which case a
737737
single value is returned.
738+
output : bool, optional
739+
Output values. Used for performance testing since the generated
740+
values are not returned.
738741
739742
Returns
740743
-------
@@ -755,6 +758,17 @@ cdef class RandomState:
755758
cdef aug_state* rng_state
756759
rng_state = &self.rng_state
757760

761+
if not output:
762+
if size is None:
763+
with self.lock:
764+
random_raw_values(rng_state)
765+
return None
766+
n = np.asarray(size).sum()
767+
with self.lock, nogil:
768+
for i in range(n):
769+
random_raw_values(rng_state)
770+
return None
771+
758772
if size is None:
759773
with self.lock:
760774
return random_raw_values(rng_state)

0 commit comments

Comments
 (0)