Skip to content

Commit f7ee784

Browse files
committed
PERF: Add accurate performance measurement
Add accurate performance measurement
1 parent c859ba8 commit f7ee784

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

ci/performance.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from collections import OrderedDict
2+
3+
import timeit
4+
5+
import numpy as np
6+
import pandas as pd
7+
from randomstate.prng import (mt19937, sfmt, dsfmt, xoroshiro128plus,
8+
xorshift1024, pcg64)
9+
10+
REPS = 3
11+
SIZE = 100000
12+
SETUP = """
13+
import numpy
14+
from numpy import array, random
15+
from randomstate.prng import (mt19937, sfmt, dsfmt, xoroshiro128plus,
16+
xorshift1024, pcg64)
17+
import randomstate
18+
import randomstate.prng
19+
20+
rs = {prng}.RandomState(123456)
21+
f = rs.__getattribute__('{fn}')
22+
args = {args}
23+
"""
24+
prngs = (np.random, mt19937, sfmt, dsfmt, xoroshiro128plus, xorshift1024, pcg64)
25+
functions = {'randint': {'low': 2 ** 31, 'dtype': 'uint32'},
26+
'random_sample': {},
27+
'random_raw': {'output': False},
28+
'standard_exponential': {},
29+
'standard_gamma': {'shape': 2.4},
30+
'standard_normal': {'method': 'zig'},
31+
'multinomial': {'n': 20, 'pvals': [1.0 / 6.0] * np.ones(6)},
32+
'negative_binomial': {'n': 5, 'p': 0.16},
33+
'poisson': {'lam': 3.0},
34+
'complex_normal': {'gamma': 2 + 0j, 'relation': 1 + 0.5j, 'method': 'zig'},
35+
'laplace': {'loc': 1, 'scale': 3},
36+
'binomial': {'n': 35, 'p': 0.25}}
37+
38+
39+
def timer(prng: str, fn: str, args: dict):
40+
if prng == 'random':
41+
# Differences with NumPy
42+
if fn in ('random_raw', 'complex_normal'):
43+
return np.nan
44+
if fn == 'standard_normal':
45+
args = {k: v for k, v in args.items() if k != 'method'}
46+
elif prng == 'mt19937' and fn == 'random_raw': # To make comparable
47+
args['size'] = 2 * args['size']
48+
setup = SETUP.format(prng=prng, fn=fn, args=args)
49+
return min(timeit.Timer('f(**args)', setup=setup).repeat(10, REPS)) / SIZE / REPS
50+
51+
52+
results = OrderedDict()
53+
for prng in prngs:
54+
name = prng.__name__.split('.')[-1]
55+
speeds = OrderedDict()
56+
for fn, args in functions.items():
57+
args['size'] = SIZE
58+
speeds[fn] = np.round(timer(name, fn, args) * 10 ** 9, 2)
59+
results[name] = pd.Series(speeds)
60+
print(name)
61+
print(results[name])
62+
63+
results = pd.DataFrame(results)
64+
results = results.loc[results.mean(1).sort_values().index]
65+
66+
index = {'randint': 'Random Integers',
67+
'random_sample': 'Uniforms',
68+
'random_raw': 'Raw',
69+
'standard_exponential': 'Exponential',
70+
'standard_gamma': 'Gamma',
71+
'standard_normal': 'Normal',
72+
'multinomial': 'Multinomial',
73+
'negative_binomial': 'Neg. Binomial',
74+
'poisson': 'Poisson',
75+
'complex_normal': 'Complex Normal',
76+
'laplace': 'Laplace',
77+
'binomial': 'Binomial'}
78+
79+
cols = {'sfmt': 'SFMT', 'dsfmt': 'dSFMT',
80+
'xoroshiro128plus': 'xoroshirt128+',
81+
'xorshift1024': 'xorshift1024', 'pcg64': 'PCG64',
82+
'mt19937': 'MT19937', 'random': 'NumPy MT19937'}
83+
84+
results.columns = [cols[c] for c in results]
85+
results.index = [index[i] for i in results.index]
86+
87+
print(results)

doc/source/change-log.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change Log
55

66
Since Version 1.13
77
------------------
8+
* Add SIMD-oriented Fast Mersenne Twister (`SFMT <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/>`) generator.
89
* Add complex normal (:meth:`~randomstate.prng.mt19937.complex_normal`)
910

1011
Version 1.13

doc/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ Individual Pseudo Random Number Generators
127127

128128
MT19937 <mt19937>
129129
dSFMT <dsfmt>
130+
SFMT <sfmt>
130131
XorShift128+ <xorshift128>
131132
XoroShiro128+ <xoroshiro128plus>
132133
XorShift1024* <xorshift1024>

doc/source/performance.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Performance
2+
-----------
3+

doc/source/sfmt.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
SFMT Randomstate
2+
****************
3+
4+
.. currentmodule:: randomstate.prng.sfmt
5+
6+
Random generator
7+
================
8+
.. autoclass::
9+
RandomState
10+
11+
.. autosummary::
12+
:toctree: generated/
13+
14+
seed
15+
get_state
16+
set_state
17+
18+
Simple random data
19+
==================
20+
.. autosummary::
21+
:toctree: generated/
22+
23+
rand
24+
randn
25+
randint
26+
random_integers
27+
random_sample
28+
random
29+
ranf
30+
sample
31+
choice
32+
bytes
33+
random_uintegers
34+
random_raw
35+
36+
Permutations
37+
============
38+
.. autosummary::
39+
:toctree: generated/
40+
41+
shuffle
42+
permutation
43+
44+
Distributions
45+
=============
46+
.. autosummary::
47+
:toctree: generated/
48+
49+
beta
50+
binomial
51+
chisquare
52+
complex_normal
53+
dirichlet
54+
exponential
55+
f
56+
gamma
57+
geometric
58+
gumbel
59+
hypergeometric
60+
laplace
61+
logistic
62+
lognormal
63+
logseries
64+
multinomial
65+
multivariate_normal
66+
negative_binomial
67+
noncentral_chisquare
68+
noncentral_f
69+
normal
70+
pareto
71+
poisson
72+
power
73+
rayleigh
74+
standard_cauchy
75+
standard_exponential
76+
standard_gamma
77+
standard_normal
78+
standard_t
79+
triangular
80+
uniform
81+
vonmises
82+
wald
83+
weibull
84+
zipf

0 commit comments

Comments
 (0)