|
| 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) |
0 commit comments