2
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?

bigbounty
  • 16,526
  • 5
  • 37
  • 65
  • 4
    Because it's not designed to generate a single random value, it's for creating numpy.ndarray objects. – juanpa.arrivillaga Jul 30 '20 at 07:11
  • @juanpa.arrivillaga A single value can also be considered as numpy.ndarray object right? – bigbounty Jul 30 '20 at 07:13
  • FYI, that's not just the case for numpy.random, but for almost anything numpy does -- if you're operating in really small spaces (e.g. small numbers of 3D vectors) then even vanilla python will usually outperform it. – Hans Musgrave Jul 30 '20 at 07:14
  • @HansMusgrave I have increased the range in the my 2nd test case. So, in my second test case, it should consume less time right? – bigbounty Jul 30 '20 at 07:15
  • @bigbounty Numpy is for vector operations. Try creating a million random numbers and you'll see the difference. – a_guest Jul 30 '20 at 07:16
  • 4
    @bigbounty almost everything in numpy has larger overhead, but pays off when you are allocating large objects (or even smallish/medium-sized). Try comparing `[rnd.randint(0,5) for _ in range(1000)]` versus `nrnd.randint(0,5,size=1000)` – juanpa.arrivillaga Jul 30 '20 at 07:17
  • @bigbounty the range is practically irrelevant. – juanpa.arrivillaga Jul 30 '20 at 07:17
  • @juanpa.arrivillaga I agree with your comment. – bigbounty Jul 30 '20 at 15:38

0 Answers0