numpy
is more efficient when generating large samples (arrays) of random numbers. For example,
In [10]: %timeit np.random.randint(1,1000000, 1000000)
5.14 ms ± 64.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [11]: %timeit [random.choice(range(1,1000000)) for _ in range(1000000)]
1.01 s ± 14.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In addition, see How can I time a code segment for testing performance with Pythons timeit? on how to perform timing tests. When you use time.clock()
, you should at least try to repeat the operation multiple times and then compute mean time. It is more advisable to use timeit
for timing tests. Also, as others have mentioned in the comments, print()
takes significantly longer that random number generation, so your timing test is mostly measuring how fast print()
works. Instead, you should do something like this:
In [12]: repeat = 1000000
...: t0 = time.clock()
...: for _ in range(repeat):
...: np.random.randint(1, 1000000)
...: t1 = time.clock()
...: print((t1 - t0) / repeat)
1.3564629999999908e-06
In [13]: repeat = 1000000
...: t2 = time.clock()
...: for _ in range(repeat):
...: random.choice(range(1, 1000000))
...: t3 = time.clock()
...: print((t3 - t2) / repeat)
1.0206699999999956e-06
So, for a single number, numpy
is on average just about 35% slower than built-in random number generator. However, previous tests show that when generating large samples, numpy
is significantly faster.