title: How can I make this C# loop faster?
body: <p><strong>Executive Summary:</strong> Reed's answer below is the fastest if you want to stay in C#.  If you're willing to marshal to C++ (which I am), that's a faster solution.</p>
  
  <p>I have two 55mb ushort arrays in C#.  I am combining them using the following loop:</p>
  
  <pre><code>float b = (float)number / 100.0f;
  for (int i = 0; i &lt; length; i++)
  {
        image.DataArray[i] = 
            (ushort)(mUIHandler.image1.DataArray[i] + 
            (ushort)(b * (float)mUIHandler.image2.DataArray[i]));
  }
  </code></pre>
  
  <p>This code, according to adding DateTime.Now calls before and afterwards, takes 3.5 seconds to run.  How can I make it faster?  </p>
  
  <p><strong>EDIT</strong>: Here is some code that, I think, shows the root of the problem.  When the following code is run in a brand new WPF application, I get these timing results:</p>
  
  <pre><code>Time elapsed: 00:00:00.4749156 //arrays added directly
  Time elapsed: 00:00:00.5907879 //arrays contained in another class
  Time elapsed: 00:00:02.8856150 //arrays accessed via accessor methods
  </code></pre>
  
  <p>So when arrays are walked directly, the time is much faster than if the arrays are inside of another object or container.  This code shows that somehow, I'm using an accessor method, rather than accessing the arrays directly.  Even so, the fastest I seem to be able to get is half a second.  When I run the second listing of code in C++ with icc, I get:</p>
  
  <pre><code>Run time for pointer walk: 0.0743338
  </code></pre>
  
  <p>In this case, then, C++ is 7x faster (using icc, not sure if the same performance can be obtained with msvc-- I'm not as familiar with optimizations there).  Is there any way to get C# near that level of C++ performance, or should I just have C# call my C++ routine?</p>
  
  <p>Listing 1, C# code:</p>
  
  <pre><code>public class ArrayHolder
  {
      int length;
      public ushort[] output;
      public ushort[] input1;
      public ushort[] input2;
      public ArrayHolder(int inLength)
      {
          length = inLength;
          output = new ushort[length];
          input1 = new ushort[length];
          input2 = new ushort[length];
      }
  
      public ushort[] getOutput() { return output; }
      public ushort[] getInput1() { return input1; }
      public ushort[] getInput2() { return input2; }
  }
  
  
  /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
  
  
          Random random = new Random();
  
          int length = 55 * 1024 * 1024;
          ushort[] output = new ushort[length];
          ushort[] input1 = new ushort[length];
          ushort[] input2 = new ushort[length];
  
          ArrayHolder theArrayHolder = new ArrayHolder(length);
  
          for (int i = 0; i &lt; length; i++)
          {
              output[i] = (ushort)random.Next(0, 16384);
              input1[i] = (ushort)random.Next(0, 16384);
              input2[i] = (ushort)random.Next(0, 16384);
              theArrayHolder.getOutput()[i] = output[i];
              theArrayHolder.getInput1()[i] = input1[i];
              theArrayHolder.getInput2()[i] = input2[i];
          }
  
          Stopwatch stopwatch = new Stopwatch(); 
          stopwatch.Start();
          int number = 44;
          float b = (float)number / 100.0f;
          for (int i = 0; i &lt; length; i++)
          {
              output[i] =
                  (ushort)(input1[i] +
                  (ushort)(b * (float)input2[i]));
          } 
          stopwatch.Stop();
  
          Console.WriteLine("Time elapsed: {0}",
              stopwatch.Elapsed);
          stopwatch.Reset();
  
          stopwatch.Start();
          for (int i = 0; i &lt; length; i++)
          {
              theArrayHolder.output[i] =
                  (ushort)(theArrayHolder.input1[i] +
                  (ushort)(b * (float)theArrayHolder.input2[i]));
          }
          stopwatch.Stop();
  
          Console.WriteLine("Time elapsed: {0}",
              stopwatch.Elapsed);
          stopwatch.Reset();
  
          stopwatch.Start();
          for (int i = 0; i &lt; length; i++)
          {
              theArrayHolder.getOutput()[i] =
                  (ushort)(theArrayHolder.getInput1()[i] +
                  (ushort)(b * (float)theArrayHolder.getInput2()[i]));
          }
          stopwatch.Stop();
  
          Console.WriteLine("Time elapsed: {0}",
              stopwatch.Elapsed);
      }
  }
  </code></pre>
  
  <p>Listing 2, C++ equivalent:
      // looptiming.cpp : Defines the entry point for the console application.
      //</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;stdlib.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  
  
  int _tmain(int argc, _TCHAR* argv[])
  {
  
      int length = 55*1024*1024;
      unsigned short* output = new unsigned short[length];
      unsigned short* input1 = new unsigned short[length];
      unsigned short* input2 = new unsigned short[length];
      unsigned short* outPtr = output;
      unsigned short* in1Ptr = input1;
      unsigned short* in2Ptr = input2;
      int i;
      const int max = 16384;
      for (i = 0; i &lt; length; ++i, ++outPtr, ++in1Ptr, ++in2Ptr){
          *outPtr = rand()%max;
          *in1Ptr = rand()%max;
          *in2Ptr = rand()%max;
      }
  
      LARGE_INTEGER ticksPerSecond;
      LARGE_INTEGER tick1, tick2;   // A point in time
      LARGE_INTEGER time;   // For converting tick into real time
  
  
      QueryPerformanceCounter(&amp;tick1);
  
      outPtr = output;
      in1Ptr = input1;
      in2Ptr = input2;
      int number = 44;
      float b = (float)number/100.0f;
  
  
      for (i = 0; i &lt; length; ++i, ++outPtr, ++in1Ptr, ++in2Ptr){
          *outPtr = *in1Ptr + (unsigned short)((float)*in2Ptr * b);
      }
      QueryPerformanceCounter(&amp;tick2);
      QueryPerformanceFrequency(&amp;ticksPerSecond);
  
      time.QuadPart = tick2.QuadPart - tick1.QuadPart;
  
      std::cout &lt;&lt; "Run time for pointer walk: " &lt;&lt; (double)time.QuadPart/(double)ticksPerSecond.QuadPart &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p><strong>EDIT 2:</strong> Enabling /QxHost in the second example drops the time down to 0.0662714 seconds.  Modifying the first loop as @Reed suggested gets me down to </p>
  
  <p>Time elapsed: 00:00:00.3835017</p>
  
  <p>So, still not fast enough for a slider.  That time is via the code:</p>
  
  <pre><code>        stopwatch.Start();
          Parallel.ForEach(Partitioner.Create(0, length),
           (range) =&gt;
           {
               for (int i = range.Item1; i &lt; range.Item2; i++)
               {
                   output[i] =
                       (ushort)(input1[i] +
                       (ushort)(b * (float)input2[i]));
               }
           });
  
          stopwatch.Stop();
  </code></pre>
  
  <p><strong>EDIT 3</strong> As per @Eric Lippert's suggestion, I've rerun the code in C# in release, and, rather than use an attached debugger, just print the results to a dialog.  They are:</p>
  
  <ul>
  <li>Simple arrays: ~0.273s</li>
  <li>Contained arrays: ~0.330s</li>
  <li>Accessor arrays: ~0.345s</li>
  <li>Parallel arrays: ~0.190s</li>
  </ul>
  
  <p>(these numbers come from a 5 run average)</p>
  
  <p>So the parallel solution is definitely faster than the 3.5 seconds I was getting before, but is still a bit under the 0.074 seconds achievable using the non-icc processor.  It seems, therefore, that the fastest solution is to compile in release and then marshal to an icc-compiled C++ executable, which makes using a slider possible here.</p>
  
  <p><strong>EDIT 4:</strong> Three more suggestions from @Eric Lippert: change the inside of the for loop from length to array.length, use doubles, and try unsafe code.</p>
  
  <p>For those three, the timing is now:</p>
  
  <ul>
  <li>length: ~0.274s</li>
  <li>doubles, not floats: ~0.290s</li>
  <li>unsafe: ~0.376s</li>
  </ul>
  
  <p>So far, the parallel solution is the big winner.  Although if I could add these via a shader, maybe I could see some kind of speedup there...</p>
  
  <p>Here's the additional code:</p>
  
  <pre><code>        stopwatch.Reset();
  
          stopwatch.Start();
  
          double b2 = ((double)number) / 100.0;
          for (int i = 0; i &lt; output.Length; ++i)
          {
              output[i] =
                  (ushort)(input1[i] +
                  (ushort)(b2 * (double)input2[i]));
          }
  
          stopwatch.Stop();
          DoubleArrayLabel.Content += "\t" + stopwatch.Elapsed.Seconds + "." + stopwatch.Elapsed.Milliseconds;
          stopwatch.Reset();
  
          stopwatch.Start();
  
          for (int i = 0; i &lt; output.Length; ++i)
          {
              output[i] =
                  (ushort)(input1[i] +
                  (ushort)(b * input2[i]));
          }
  
          stopwatch.Stop();
          LengthArrayLabel.Content += "\t" + stopwatch.Elapsed.Seconds + "." + stopwatch.Elapsed.Milliseconds;
          Console.WriteLine("Time elapsed: {0}",
              stopwatch.Elapsed);
          stopwatch.Reset();
  
          stopwatch.Start();
          unsafe
          {
              fixed (ushort* outPtr = output, in1Ptr = input1, in2Ptr = input2){
                  ushort* outP = outPtr;
                  ushort* in1P = in1Ptr;
                  ushort* in2P = in2Ptr;
                  for (int i = 0; i &lt; output.Length; ++i, ++outP, ++in1P, ++in2P)
                  {
                      *outP = (ushort)(*in1P + b * (float)*in2P);
                  }
              }
          }
  
          stopwatch.Stop();
          UnsafeArrayLabel.Content += "\t" + stopwatch.Elapsed.Seconds + "." + stopwatch.Elapsed.Milliseconds;
          Console.WriteLine("Time elapsed: {0}",
              stopwatch.Elapsed);
  </code></pre>