[Numpy-discussion] python's random.random() faster than numpy.random.rand() ???

Robert Kern robert.kern at gmail.com
Sat Jan 27 16:08:27 CST 2007


Mark P. Miller wrote:
> Greetings:
> 
> I've recently been working with numpy's random number generators and 
> noticed that python's core random number generator is faster than 
> numpy's for the uniform distribution.
> 
> In other words,
> 
> for a in range(1000000):
>      b = random.random()    #core python code
> 
> is substantially faster than
> 
> for a in range(1000000):
>      b = numpy.random.rand()    #numpy code
> 
> 
> For other distributions that I've worked with, numpy's random number 
> generators are much faster than the core python generators.

Yes, the standard library's non-uniform generators are implemented in Python.
I'm not entirely sure where we are losing time in numpy, though. Perhaps the
generated Pyrex code for the interface is suboptimal. Or perhaps we ought to
compile with greater optimization.

> Any thoughts?  Just wanted to mention it to you.  Or should I be using 
> some other approach to call numpy's uniform generator?

If you need a lot of values, request a lot of values all at once.

  numpy.random.rand(1000000)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list