Skip to content

Commit 46c668b

Browse files
authored
Merge pull request #79 from bashtage/refactor-uintegers
REF: Refactor uintegers
2 parents 18b4eee + f446303 commit 46c668b

File tree

3 files changed

+32
-1584
lines changed

3 files changed

+32
-1584
lines changed

randomstate/performance.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import struct
33
import timeit
44

5-
import pandas as pd
65
import numpy as np
6+
import pandas as pd
77
from numpy.random import RandomState
88

99
rs = RandomState()
@@ -44,7 +44,7 @@ def run_timer(dist, command, numpy_command=None, setup='', random_type=''):
4444
mod = 'randomstate.prng' if rng != 'random' else 'numpy'
4545
key = '-'.join((mod, rng, dist)).replace('"', '')
4646
command = numpy_command if 'numpy' in mod else command
47-
res[key] = timer(command.format(dist=dist), setup=setup.format(mod=mod, rng=rng))
47+
res[key] = timer(command, setup=setup.format(mod=mod, rng=rng))
4848

4949
s = pd.Series(res)
5050
t = s.apply(lambda x: '{0:0.2f} ms'.format(x))
@@ -68,41 +68,41 @@ def run_timer(dist, command, numpy_command=None, setup='', random_type=''):
6868

6969
def timer_uniform():
7070
dist = 'random_sample'
71-
command = 'rs.{dist}(1000000)'
71+
command = 'rs.random_sample(1000000)'
7272
run_timer(dist, command, None, SETUP, 'Uniforms')
7373

7474

7575
def timer_32bit():
7676
info = np.iinfo(np.uint32)
7777
min, max = info.min, info.max
78-
dist = 'randint'
79-
command = 'rs.{dist}({min}, {max}+1, 1000000, dtype=np.uint64)'
80-
command = command.format(dist='{dist}', min=min, max=max)
81-
command_numpy = command
82-
run_timer(dist, command, None, SETUP, '32-bit unsigned integers')
78+
dist = 'random_uintegers'
79+
command = 'rs.random_uintegers(1000000, 32)'
80+
command_numpy = 'rs.randint({min}, {max}+1, 1000000, dtype=np.uint32)'
81+
command_numpy = command_numpy.format(min=min, max=max)
82+
run_timer(dist, command, command_numpy, SETUP, '32-bit unsigned integers')
8383

8484

8585
def timer_64bit():
8686
info = np.iinfo(np.uint64)
8787
min, max = info.min, info.max
88-
dist = 'randint'
89-
command = 'rs.{dist}({min}, {max}+1, 1000000, dtype=np.uint64)'
90-
command = command.format(dist='{dist}', min=min, max=max)
91-
command_numpy = command
92-
run_timer(dist, command, None, SETUP, '64-bit unsigned integers')
88+
dist = 'random_uintegers'
89+
command = 'rs.random_uintegers(1000000)'
90+
command_numpy = 'rs.randint({min}, {max}+1, 1000000, dtype=np.uint64)'
91+
command_numpy = command_numpy.format(min=min, max=max)
92+
run_timer(dist, command, command_numpy, SETUP, '64-bit unsigned integers')
9393

9494

9595
def timer_normal():
96-
command = 'rs.{dist}(1000000, method="bm")'
97-
command_numpy = 'rs.{dist}(1000000)'
9896
dist = 'standard_normal'
97+
command = 'rs.standard_normal(1000000, method="bm")'
98+
command_numpy = 'rs.standard_normal(1000000)'
9999
run_timer(dist, command, command_numpy, SETUP, 'Box-Muller normals')
100100

101101

102102
def timer_normal_zig():
103-
command = 'rs.{dist}(1000000, method="zig")'
104-
command_numpy = 'rs.{dist}(1000000)'
105103
dist = 'standard_normal'
104+
command = 'rs.standard_normal(1000000, method="zig")'
105+
command_numpy = 'rs.standard_normal(1000000)'
106106
run_timer(dist, command, command_numpy, SETUP, 'Standard normals (Ziggurat)')
107107

108108

randomstate/randomstate.pyx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -633,34 +633,36 @@ cdef class RandomState:
633633
These should not be used to produce bounded random numbers by
634634
simple truncation.
635635
"""
636-
cdef uint32_t [:] randoms32
637-
cdef uint64_t [:] randoms64
638-
cdef aug_state* rng_state
639-
cdef Py_ssize_t i, n
640-
rng_state = &self.rng_state
636+
cdef np.npy_intp i, n
637+
cdef np.ndarray array
638+
cdef uint32_t* data32
639+
cdef uint64_t* data64
640+
cdef aug_state* rng_state = &self.rng_state
641641
if bits == 64:
642642
if size is None:
643643
with self.lock:
644644
return random_uint64(rng_state)
645-
n = compute_numel(size)
646-
randoms64 = np.empty(n, np.uint64)
645+
array = <np.ndarray>np.empty(size, np.uint64)
646+
n = np.PyArray_SIZE(array)
647+
data64 = <uint64_t *>np.PyArray_DATA(array)
647648
with self.lock, nogil:
648649
for i in range(n):
649-
randoms64[i] = random_uint64(rng_state)
650-
return np.asarray(randoms64).reshape(size)
650+
data64[i] = random_uint64(rng_state)
651651
elif bits == 32:
652652
if size is None:
653653
with self.lock:
654654
return random_uint32(rng_state)
655-
n = compute_numel(size)
656-
randoms32 = np.empty(n, np.uint32)
655+
array = <np.ndarray>np.empty(size, np.uint32)
656+
n = np.PyArray_SIZE(array)
657+
data32 = <uint32_t *>np.PyArray_DATA(array)
657658
with self.lock, nogil:
658659
for i in range(n):
659-
randoms32[i] = random_uint32(rng_state)
660-
return np.asarray(randoms32).reshape(size)
660+
data32[i] = random_uint32(rng_state)
661661
else:
662662
raise ValueError('Unknown value of bits. Must be either 32 or 64.')
663663

664+
return array
665+
664666
def random_raw(self, size=None):
665667
"""
666668
random_raw(self, size=None)

0 commit comments

Comments
 (0)