title: Parallel execution speed of a vectorized function using numba
body: <p>Years ago I wrote a math library with <code>numba</code> to do vectorized operations on large arrays of elements (example: multiply two array of matrices). I recently opened it up to experiment with parallelism (feature which did not exist at the time). In doing so there was a speed improvement but not as much as I expected.</p>
  <p>To further understand, I wrote this basic example:</p>
  <pre><code>import numpy as np
  from numba import njit, prange, float64
  
  @njit(float64[:] (float64[:], float64[:]), parallel=True)
  def array_sum(array0, array1):
      &quot;&quot;&quot;
      Sum elements of array0 to array1, similar to doing + on two numpy arrays
      &quot;&quot;&quot;
  
      result = np.empty(array0.shape)
  
      for i in prange(array0.shape[0]):
          result[i] = array0[i] + array1[i]
  
      return result
  </code></pre>
  <p>And then tested with:</p>
  <pre><code>import time
  v0 = np.random.random(10**7) # 10 million floats
  v1 = np.random.random(10**7) # 10 million floats
  array_sum(v0,v1) # run this once so the code compiles
  
  #--- test start ---#
  start_time = time.time()
  array_sum(v0,v1) # do the work
  end_time = time.time()
  delta = end_time-start_time
  print (delta) 
  </code></pre>
  <p>On my machine I get the following results:</p>
  <pre><code># direct numpy array0+array1 ---&gt; 0.016865015029907227
  # parallel=False             ---&gt; 0.016468048095703125
  # parallel=True              ---&gt; 0.010709047317504883
  </code></pre>
  <p>With <code>parallel=False</code> the speed I get matches <code>numpy</code>'s, which is what I expected. And with <code>parallel=True</code> I got a ~1.54x speed increase, which is nice, but for a simple operation like this I was expecting a larger impact on my 8 core machine.</p>
  <p><strong>QUESTION</strong></p>
  <p>Can parallelism be used in a more effective way to do vectorized operations similar to the example above?</p>