title: Python multiprocessing not yielding the speed-up expected
body: <p>I am trying to optimize my code using Python's <code>multiprocessing.Pool</code> module, but I am not getting the speed-up results that I would logically expect.</p>
  
  <p>The main method I am doing involves calculating matrix-vector products for a large number of vectors and a fixed large sparse matrix. Below is a toy example which performs what I need, but with random matrices. </p>
  
  <pre><code>import time
  import numpy as np
  import scipy.sparse as sp
  
  def calculate(vector, matrix = None):
      for i in range(50):
          v = matrix.dot(vector)
      return v
  
  if __name__ == '__main__':
      N = 1e6
      matrix = sp.rand(N, N, density = 1e-5, format = 'csr')
      t = time.time()
      res = []
      for i in range(10):
          res.append(calculate(np.random.rand(N), matrix = matrix))    
      print time.time() - t
  </code></pre>
  
  <p>The method terminates in about <code>30</code> seconds.</p>
  
  <p>Now, since the calculation of each element of <code>results</code> does not depend on the results of any other calculation, it is natural to think that paralel calculation will speed up the process. The idea is to create 4 processes and if each does some of the calculations, then the time it takes for all the processes to complete should decrease by some factor around <code>4</code>. To do this, I wrote the following code:</p>
  
  <pre><code>import time
  import numpy as np
  import scipy.sparse as sp
  from multiprocessing import Pool
  from functools import partial
  
  def calculate(vector, matrix = None):
      for i in range(50):
          v = matrix.dot(vector)
      return v
  
  if __name__ == '__main__':
      N = 1e6
      matrix = sp.rand(N, N, density = 1e-5, format = 'csr')
  
      t = time.time()
      input = []
      for i in range(10):
          input.append(np.random.rand(N))
      mp = partial(calculate, matrix = matrix)
      p = Pool(4)
      res = p.map(mp, input)
      print time.time() - t
  </code></pre>
  
  <p>My problem is that this code takes slightly above <code>20</code> seconds to run, so I did not even improve performance by a factor of <code>2</code>! Even worse, the performance does <em>not</em> improve even if the pool contains <code>8</code> processes! Any idea why the speed-up is not happening?</p>
  
  <hr>
  
  <p><strong>Note</strong>: My actual method takes much longer, and the input vectors are stored in a file. If I split the file in <code>4</code> pieces and then run my script in a separate process for each file manually, each process terminates four times as quickly as it would for the whole file (as expected). I am confuzed why this speed-up (which is obviously possible) is not happening with <code>multiprocessing.Pool</code></p>
  
  <hr>
  
  <p><strong>Edi</strong>: I have just found <a href="https://stackoverflow.com/questions/15414027/multiprocessing-pool-makes-numpy-matrix-multiplication-slower">Multiprocessing.Pool makes Numpy matrix multiplication slower</a> this question which may be related. I have to check, though.</p>