title: how to speed up enumerate for numpy array / how to enumerate over numpy array efficiently?
body: <p>I need to generate a lot of random numbers. I've tried using <code>random.random</code> but this function is quite slow. Therefore I switched to <code>numpy.random.random</code> which is way faster! So far so good. The generated random numbers are actually used to calculate some thing (based on the number). I therefore <code>enumerate</code> over each number and replace the value. This seems to kill all my previously gained speedup. Here are the stats generated with <code>timeit()</code>:</p>
  
  <pre><code>test_random - no enumerate
  0.133111953735
  test_np_random - no enumerate
  0.0177130699158
  
  
  test_random - enumerate
  0.269361019135
  test_np_random - enumerate
  1.22525310516
  </code></pre>
  
  <p>as you can see, generating the number is almost 10 times faster using numpy, but enumerating over those numbers gives me equal run times.</p>
  
  <p>Below is the code that I'm using:</p>
  
  <pre><code>import numpy as np
  import timeit
  import random
  
  NBR_TIMES = 10
  NBR_ELEMENTS = 100000
  
  def test_random(do_enumerate=False):
      y = [random.random() for i in range(NBR_ELEMENTS)]
      if do_enumerate:
          for index, item in enumerate(y):
              # overwrite the y value, in reality this will be some function of 'item'
              y[index] = 1 + item
  
  def test_np_random(do_enumerate=False):
      y = np.random.random(NBR_ELEMENTS)
      if do_enumerate:
          for index, item in enumerate(y):
              # overwrite the y value, in reality this will be some function of 'item'
              y[index] = 1 + item
  
  if __name__ == '__main__':
      from timeit import Timer
  
      t = Timer("test_random()", "from __main__ import test_random")
      print "test_random - no enumerate"
      print t.timeit(NBR_TIMES)
  
      t = Timer("test_np_random()", "from __main__ import test_np_random")
      print "test_np_random - no enumerate"
      print t.timeit(NBR_TIMES)
  
  
      t = Timer("test_random(True)", "from __main__ import test_random")
      print "test_random - enumerate"
      print t.timeit(NBR_TIMES)
  
      t = Timer("test_np_random(True)", "from __main__ import test_np_random")
      print "test_np_random - enumerate"
      print t.timeit(NBR_TIMES)
  </code></pre>
  
  <p>What's the best way to speed this up and why does <code>enumerate</code> slow things down so dramatically?</p>
  
  <p><strong>EDIT:</strong> the reason I use <code>enumerate</code> is because I need both the index and the value of the current element.</p>