title: Efficient pairwise DTW calculation using numpy or cython
body: <p>I am trying to calculate the pairwise distances between multiple time-series contained in a numpy array. Please see the code below</p>
  
  <pre><code>print(type(sales))
  print(sales.shape)
  
  &lt;class 'numpy.ndarray'&gt;
  (687, 157)
  </code></pre>
  
  <p>So, <code>sales</code> contains 687 time series of length 157. Using pdist to calculate the DTW distances between the time series.</p>
  
  <pre><code>import fastdtw
  import scipy.spatial.distance as sd
  
  def my_fastdtw(sales1, sales2):
      return fastdtw.fastdtw(sales1,sales2)[0]
  
  distance_matrix = sd.pdist(sales, my_fastdtw)
  </code></pre>
  
  <p><strong>---EDIT: tried doing it without <code>pdist()</code>-----</strong></p>
  
  <pre><code>distance_matrix = []
  m = len(sales)    
  for i in range(0, m - 1):
      for j in range(i + 1, m):
          distance_matrix.append(fastdtw.fastdtw(sales[i], sales[j]))
  </code></pre>
  
  <p><strong>---EDIT: parallelizing the inner for loop-----</strong></p>
  
  <pre><code>from joblib import Parallel, delayed
  import multiprocessing
  import fastdtw
  
  num_cores = multiprocessing.cpu_count() - 1
  N = 687
  
  def my_fastdtw(sales1, sales2):
      return fastdtw.fastdtw(sales1,sales2)[0]
  
  results = [[] for i in range(N)]
  for i in range(0, N- 1):
      results[i] = Parallel(n_jobs=num_cores)(delayed(my_fastdtw) (sales[i],sales[j])  for j in range(i + 1, N) )
  </code></pre>
  
  <p>All the methods are very slow. The parallel method takes around 12 minutes. Can someone please suggest an efficient way?</p>
  
  <p>---EDIT: Following the steps mentioned in the answer below---</p>
  
  <p>Here is how the lib folder looks like:</p>
  
  <pre><code>VirtualBox:~/anaconda3/lib/python3.6/site-packages/fastdtw-0.3.2-py3.6- linux-x86_64.egg/fastdtw$ ls
  _fastdtw.cpython-36m-x86_64-linux-gnu.so  fastdtw.py   __pycache__
  _fastdtw.py                               __init__.py
  </code></pre>
  
  <p>So, there is a cython version of fastdtw in there. While installation, I did not receive any errors. Even now, when I pressed <code>CTRL-C</code> during my program execution, I can see that the pure python version is being used (<code>fastdtw.py</code>):</p>
  
  <pre><code>/home/vishal/anaconda3/lib/python3.6/site-packages/fastdtw/fastdtw.py in fastdtw(x, y, radius, dist)
  
  /home/vishal/anaconda3/lib/python3.6/site-packages/fastdtw/fastdtw.py in __fastdtw(x, y, radius, dist)
  </code></pre>
  
  <p>The code remains slow like before.</p>