title: Performance difference between float and double in x86 and x86_64
body: <p>A while ago I heard that some compilers use SSE2 extensions for floating point operations for x86_64 architecture, so I used this simple code to determine the performance difference between them.</p>
  
  <p>I disabled Intel SpeedStep technology via BIOS and system load was approximately equal for my tests. I am using GCC 4.8 on OpenSuSE 64 bit.</p>
  
  <p>I am writing a program with a lot of FPU operations and I would like to know if this test is valid or not?</p>
  
  <p>And any information about the performance difference between <code>float</code> and <code>double</code> under each architecture is appreciated.</p>
  
  <p>Code :</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sys/time.h&gt;                
  #include &lt;vector&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  
  int main()
  {
      timeval t1, t2;
      double elapsedTime;
  
      double TotalTime = 0;
  
  
      for(int j=0 ; j &lt; 100 ; j++)
      {
          // start timer
          gettimeofday(&amp;t1, NULL);
  
          vector&lt;float&gt; RealVec;
          float temp;
  
          for (int i = 0; i &lt; 1000000; i++)
          {
              temp = static_cast &lt;float&gt; (rand()) / (static_cast &lt;float&gt; (RAND_MAX));
              RealVec.push_back(temp);
          }
  
          for (int i = 0; i &lt; 1000000; i++)
              {
                  RealVec[i] = (RealVec[i]*2-435.345345)/15.75;
              }
  
          // stop timer
          gettimeofday(&amp;t2, NULL);
          elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
          elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
  
          TotalTime = TotalTime + elapsedTime;
      }
  
  
      cout &lt;&lt; TotalTime/100 &lt;&lt; " ms.\n";
  
      return 0;
  }
  </code></pre>
  
  <p>and result :</p>
  
  <p>32 Bit Double</p>
  
  <p>157.781 ms.
  151.994 ms.
  152.244 ms.</p>
  
  <p>32 Bit Float</p>
  
  <p>149.896 ms.
  148.489 ms.
  161.086 ms.</p>
  
  <p>64 Bit Double</p>
  
  <p>110.125 ms.
  111.612 ms.
  113.818 ms.</p>
  
  <p>64 Bit Float</p>
  
  <p>110.393 ms.
  106.778 ms.
  107.833 ms.</p>