Skip to content

Commit 0afc584

Browse files
Sheppard, KevinSheppard, Kevin
authored andcommitted
FIX: Fix performance.py to work with module structure
Further reorganization of module Fix performance.py to work with new structure
1 parent 44dba31 commit 0afc584

File tree

5 files changed

+65
-71
lines changed

5 files changed

+65
-71
lines changed

README.md

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -67,101 +67,89 @@ python setup.py install
6767

6868
## Building for Testing Purposes
6969

70-
There are two options. The first will build a library for xorshift128 called
71-
`core_rng`.
70+
This command will build a single module containining xorshift128 called
71+
`interface`.
7272

7373
```bash
7474
cd randomstate
7575
python setup-basic.py build_ext --inplace
7676
```
7777

78-
The second will build a numberfiles, one for each RNG.
79-
80-
```bash
81-
cd randomstate
82-
python setup-all.py build_ext --inplace
83-
```
84-
8578
## Using
86-
If you installed,
79+
80+
The separate generators are importable from `randomstate.prng`.
8781

8882
```python
89-
import randomstate.xorshift128
90-
rs = randomstate.xorshift128.RandomState()
83+
import randomstate
84+
rs = randomstate.prng.xorshift128.RandomState()
9185
rs.random_sample(100)
9286

93-
import randomstate.pcg64
94-
rs = randomstate.pcg64.RandomState()
87+
rs = randomstate.prng.pcg64.RandomState()
9588
rs.random_sample(100)
9689

9790
# Identical to NumPy
98-
import randomstate.mt19937
99-
rs = randomstate.mt19937.RandomState()
91+
rs = randomstate.prng.mt19937.RandomState()
10092
rs.random_sample(100)
10193
```
10294

103-
If you use `setup-basic.py`,
95+
Like NumPy, `randomstate` also exposes a single instance of the `mt19937`
96+
generator directly at the moduel level so that commands like
10497

10598
```python
106-
import core_rng
107-
108-
rs = core_rng.RandomState()
109-
rs.random_sample(100)
99+
import randomstate
100+
randomstate.standard_normal()
101+
randomstate.exponential(1.0, 1.0, size=10)
110102
```
111103

112-
If you use `setup-all.py`,
113-
114-
```python
115-
import mt19937, pcg32, xorshift128
104+
will work.
116105

117-
rs = mt19937.RandomState()
118-
rs.random_sample(100)
106+
If you use `setup-basic.py`,
119107

120-
rs = pcg32.RandomState()
121-
rs.random_sample(100)
108+
```python
109+
import interface
122110

123-
rs = xorshift129.RandomState()
111+
rs = interface.RandomState()
124112
rs.random_sample(100)
125113
```
126114

127115
## License
128116
Standard NCSA, plus sub licenses for components.
129117

130118
## Performance
131-
Performance is promising. Some early numbers:
119+
Performance is promising, and even the mt19937 seems to be faster than NumPy's mt19937.
132120

133121
```
134-
Time to produce 1,000,000 Uniforms
122+
Time to produce 1,000,000 Standard normals
135123
************************************************************
136-
numpy-random-random_sample 13.68 ms
137-
randomstate-mlfg_1279_861-random_sample 6.64 ms
138-
randomstate-mrg32k3a-random_sample 37.87 ms
139-
randomstate-mt19937-random_sample 13.33 ms
140-
randomstate-pcg32-random_sample 10.20 ms
141-
randomstate-pcg64-random_sample 7.83 ms
142-
randomstate-xorshift1024-random_sample 6.20 ms
143-
randomstate-xorshift128-random_sample 5.49 ms
144-
145-
Uniforms per second
124+
numpy-random-standard_normal 58.34 ms
125+
randomstate.prng-mlfg_1279_861-standard_normal 46.20 ms
126+
randomstate.prng-mrg32k3a-standard_normal 75.95 ms
127+
randomstate.prng-mt19937-standard_normal 52.68 ms
128+
randomstate.prng-pcg32-standard_normal 48.38 ms
129+
randomstate.prng-pcg64-standard_normal 46.27 ms
130+
randomstate.prng-xorshift1024-standard_normal 45.53 ms
131+
randomstate.prng-xorshift128-standard_normal 45.57 ms
132+
133+
Standard normals per second
146134
************************************************************
147-
numpy-random-random_sample 73.11 million
148-
randomstate-mlfg_1279_861-random_sample 150.71 million
149-
randomstate-mrg32k3a-random_sample 26.41 million
150-
randomstate-mt19937-random_sample 75.03 million
151-
randomstate-pcg32-random_sample 98.00 million
152-
randomstate-pcg64-random_sample 127.77 million
153-
randomstate-xorshift1024-random_sample 161.39 million
154-
randomstate-xorshift128-random_sample 182.29 million
135+
numpy-random-standard_normal 17.14 million
136+
randomstate.prng-mlfg_1279_861-standard_normal 21.65 million
137+
randomstate.prng-mrg32k3a-standard_normal 13.17 million
138+
randomstate.prng-mt19937-standard_normal 18.98 million
139+
randomstate.prng-pcg32-standard_normal 20.67 million
140+
randomstate.prng-pcg64-standard_normal 21.61 million
141+
randomstate.prng-xorshift1024-standard_normal 21.96 million
142+
randomstate.prng-xorshift128-standard_normal 21.94 million
155143
156144
Speed-up relative to NumPy
157145
************************************************************
158-
randomstate-mlfg_1279_861-random_sample 106.1%
159-
randomstate-mrg32k3a-random_sample -63.9%
160-
randomstate-mt19937-random_sample 2.6%
161-
randomstate-pcg32-random_sample 34.0%
162-
randomstate-pcg64-random_sample 74.8%
163-
randomstate-xorshift1024-random_sample 120.7%
164-
randomstate-xorshift128-random_sample 149.3%
146+
randomstate.prng-mlfg_1279_861-standard_normal 26.3%
147+
randomstate.prng-mrg32k3a-standard_normal -23.2%
148+
randomstate.prng-mt19937-standard_normal 10.8%
149+
randomstate.prng-pcg32-standard_normal 20.6%
150+
randomstate.prng-pcg64-standard_normal 26.1%
151+
randomstate.prng-xorshift1024-standard_normal 28.1%
152+
randomstate.prng-xorshift128-standard_normal 28.0%
165153
166154
--------------------------------------------------------------------------------
167155
```
@@ -170,8 +158,12 @@ randomstate-xorshift128-random_sample 149.3%
170158

171159
### New Functions
172160

161+
* `random_entropy` - Read from the system entropy provider, which is commonly
162+
used in cryptographic applications
173163
* `random_uintegers` - unsigned integers `[0, 2**64-1]`
174164
* `jump` - Jumps RNGs that support it. `jump` moves the state a great
175165
distance. _Only available if supported by the RNG._
176166
* `advance` - Advanced the core RNG 'as-if' a number of draws were made,
177167
without actually drawing the numbers. _Only available if supported by the RNG._
168+
169+
### New Functions

randomstate/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
from __future__ import division, absolute_import, print_function
22

33
from randomstate.prng.mt19937.mt19937 import *
4-
from randomstate.prng.mlfg_1279_861 import mlfg_1279_861
5-
from randomstate.prng.mrg32k3a import mrg32k3a
6-
from randomstate.prng.pcg32 import pcg32
7-
from randomstate.prng.pcg64 import pcg64
8-
from randomstate.prng.xorshift1024 import xorshift1024
9-
from randomstate.prng.xorshift128 import xorshift128
4+
import randomstate.prng

randomstate/interface.pyx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ cdef extern from "distributions.h":
117117
include "array_utilities.pxi"
118118
include "bounded_integers.pxi"
119119

120-
def read_entropy(size=None):
120+
def random_entropy(size=None):
121121
"""
122+
random_entropy(size=None)
123+
122124
Read entropy from the system cryptographic provider
123125
124126
Parameters
@@ -138,6 +140,10 @@ def read_entropy(size=None):
138140
On Unix-like machines, reads from /dev/urandom. On Windows machines reads
139141
from the RSA Full cryptographic service provider.
140142
143+
This function reads from the system entropy pool and so samples are
144+
not reproducible. In particular, it does *NOT* make use of a
145+
RandomState, and so seed, get_state and set_state have no effect.
146+
141147
Raises RuntimeError if the command fails.
142148
"""
143149
cdef bint success

randomstate/performance.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
rs.random_sample()
1010
'''
1111

12-
SETUP_RS = '''
13-
import {mod}.{rng}
14-
rs = {mod}.{rng}.RandomState()
15-
rs.random_sample()
16-
'''
17-
1812
scale_32 = scale_64 = 1
1913
if sys.maxsize < 2 ** 32:
2014
# 32 bit
@@ -40,7 +34,7 @@ def run_timer(dist, command, numpy_command=None, setup='', random_type=''):
4034

4135
res = {}
4236
for rng in RNGS:
43-
mod = 'randomstate.prng.' + rng if rng != 'random' else 'numpy'
37+
mod = 'randomstate.prng' if rng != 'random' else 'numpy'
4438
key = '-'.join((mod, rng, dist)).replace('"', '')
4539
command = numpy_command if 'numpy' in mod else command
4640
res[key] = timer(command.format(dist=dist), setup=setup.format(mod=mod, rng=rng))

randomstate/prng/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from randomstate.prng.mt19937 import mt19937
2+
from randomstate.prng.mlfg_1279_861 import mlfg_1279_861
3+
from randomstate.prng.mrg32k3a import mrg32k3a
4+
from randomstate.prng.pcg32 import pcg32
5+
from randomstate.prng.pcg64 import pcg64
6+
from randomstate.prng.xorshift1024 import xorshift1024
7+
from randomstate.prng.xorshift128 import xorshift128

0 commit comments

Comments
 (0)