title: How to make my C# code faster to compute dot product
body: <p>I'm trying to implement neural network and deep learning code in C#. Sample code in my text book is written in Python, so I'm trying to convert them to C#.</p>
  
  <p>My question is that calculating dot product with numpy is extremely faster than my C# code written from scratch.</p>
  
  <p>While my numpy code takes a few second to calculate dot product 1000 times, my C# code takes much longer than it.</p>
  
  <p>Here is my question. How can I make my C# code faster?</p>
  
  <p>Here is numpy code:</p>
  
  <pre><code>C:\temp&gt;more dot.py
  from datetime import datetime
  
  import numpy as np
  
  W = np.random.randn(784, 100)
  x = np.random.randn(100, 784)
  
  print(datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
  
  for i in range(0,1000):
      np.dot(x, W)
  
  print(datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
  
  C:\temp&gt;\Python35\python.exe dot.py
  2017/02/08 00:49:14
  2017/02/08 00:49:16
  C:\temp&gt;
  </code></pre>
  
  <p>And this is C# code:</p>
  
  <pre><code>public static double[,] dot(double[,] a, double[,] b)
  {
      double[,] dot = new double[a0, b1];
  
      for (int i = 0; i &lt; a.GetLength(0); i++)
      {
          for (int j = 0; j &lt; b.GetLength(1); j++)
          {
              // the next loop looks way slow according to the profiler
              for (int k = 0; k &lt; b.GetLength(0); k++)
                  dot[i, j] += a[i, k] * b[k, j];
          }
      }
      return dot;
  }
  
  static void Main(string[] args)
  {
      // compatible function with np.random.randn()
      double[,] W = random_randn(784, 100);
      double[,] x = random_randn(100, 784);
  
      Console.WriteLine(DateTime.Now.ToString("F"));
      for (int i = 0; i &lt; 1000; i++)
          dot(W, x);
      Console.WriteLine(DateTime.Now.ToString("F"));
  }
  </code></pre>
  
  <p>Regards,</p>