In [27]: import random as rnd
In [28]: from numpy import random as nrnd
In [29]: %timeit rnd.randint(0,5)
859 ns ± 22.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [30]: %timeit nrnd.randint(0,5)
4.53 µs ± 104 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [31]: %timeit nrnd.randint(0,10000000)
4.67 µs ± 116 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [32]: %timeit rnd.randint(0,10000000)
979 ns ± 25.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.
In my 2nd test case, I have increased the random range to (0,10000000)
. As the range is larger, numpy should perform better at this test case.
Why is numpy.random.randint()
taking more time than built in random.randint
? What extra operations is numpy
doing here?
Aucun commentaire:
Enregistrer un commentaire