/fastrand

Fast random number generation in Python

Primary LanguageCApache License 2.0Apache-2.0

fastrand

Fast random number generation in Python

Blog post: Ranged random-number generation is slow in Python…

Requirements: Linux-like system (including a Mac), a C compiler, Python.

python setup.py build
python setup.py install --home=$HOME
PYTHONPATH=$PYTHONPATH:~/lib/python

It is nearly an order of magnitude faster than the alternatives:

python -m timeit -s 'import fastrand' 'fastrand.pcg32bounded(1001)'
10000000 loops, best of 3: 0.0914 usec per loop
python -m timeit -s 'import random' 'random.randint(0,1000)'
1000000 loops, best of 3: 0.698 usec per loop
python -m timeit -s 'import numpy' 'numpy.random.randint(0, 1000)'
1000000 loops, best of 3: 0.795 usec per loop

future work

Also look at https://github.com/rkern/line_profiler