title: Integer vs double arithmetic performance?
body: <p>i'm writing a C# class to perform 2D separable convolution using integers to obtain better performance than double counterpart. The problem is that i don't obtain a real performance gain.</p>
  
  <p>This is the X filter code (it is valid both for int and double cases):  </p>
  
  <pre><code>foreach (pixel)
  {
        int value = 0;
        for (int k = 0; k &lt; filterOffsetsX.Length; k++)
        {
            value += InputImage[index + filterOffsetsX[k]] * filterValuesX[k];  //index is relative to current pixel position
        }
        tempImage[index] = value;
   }
  </code></pre>
  
  <p>In the integer case "value", "InputImage" and "tempImage" are of "int", "Image<code>&lt;byte&gt;</code>" and "Image<code>&lt;int&gt;</code>" types.<br>
  In the double case "value", "InputImage" and "tempImage" are of "double", "Image<code>&lt;double&gt;</code>" and "Image<code>&lt;double&gt;</code>" types.<br>
  (filterValues is int[] in each case)<br>
  (The class Image<code>&lt;T&gt;</code> is part of an extern dll. It should be similar to .NET Drawing Image class..).</p>
  
  <p>My goal is to achieve fast perfomance thanks to int += (byte * int) vs double += (double * int)  </p>
  
  <p>The following times are mean of 200 repetitions.<br>
  Filter size 9  =  0.031 (double)    0.027 (int)<br>
  Filter size 13  = 0.042 (double)    0.038 (int)<br>
  Filter size 25  = 0.078  (double)   0.070  (int)  </p>
  
  <p>The performance gain is minimal. Can this be caused by pipeline stall and suboptimal code?</p>
  
  <p>EDIT: simplified the code deleting unimportant vars.  </p>
  
  <p>EDIT2: i don't think i have a cache miss related problema because "index"iterate through adjacent memory cells (row after row fashion). Moreover "filterOffstetsX" contains only small offsets relatives to pixels on the same row and at a max distance of filter size / 2. The problem can be present in the second separable filter (Y-filter) but times are not so different.</p>