title: Floating point vs integer calculations on modern hardware
body: <p>I am doing some performance critical work in C++, and we are currently using integer calculations for problems that are inherently floating point because "its faster". This causes a whole lot of annoying problems and adds a lot of annoying code.</p>
  
  <p>Now, I remember reading about how floating point calculations were so slow approximately circa the 386 days, where I believe (IIRC) that there was an optional co-proccessor. But surely nowadays with exponentially more complex and powerful CPUs it makes no difference in "speed" if doing floating point or integer calculation? Especially since the actual calculation time is tiny compared to something like causing a pipeline stall or fetching something from main memory?</p>
  
  <p>I know the correct answer is to benchmark on the target hardware, what would be a good way to test this? I wrote two tiny C++ programs and compared their run time with "time" on Linux, but the actual run time is too variable (doesn't help I am running on a virtual server). Short of spending my entire day running hundreds of benchmarks, making graphs etc. is there something I can do to get a reasonable test of the relative speed? Any ideas or thoughts? Am I completely wrong?</p>
  
  <p>The programs I used as follows, they are not identical by any means:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;cstdlib&gt;
  #include &lt;time.h&gt;
  
  int main( int argc, char** argv )
  {
      int accum = 0;
  
      srand( time( NULL ) );
  
      for( unsigned int i = 0; i &lt; 100000000; ++i )
      {
          accum += rand( ) % 365;
      }
      std::cout &lt;&lt; accum &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Program 2:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;cstdlib&gt;
  #include &lt;time.h&gt;
  
  int main( int argc, char** argv )
  {
  
      float accum = 0;
      srand( time( NULL ) );
  
      for( unsigned int i = 0; i &lt; 100000000; ++i )
      {
          accum += (float)( rand( ) % 365 );
      }
      std::cout &lt;&lt; accum &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p>Edit: The platform I care about is regular x86 or x86-64 running on desktop Linux and Windows machines.</p>
  
  <p>Edit 2(pasted from a comment below): We have an extensive code base currently. Really I have come up against the generalization that we "must not use float since integer calculation is faster"  - and I am looking for a way (if this is even true) to disprove this generalized assumption. I realize that it would be impossible to predict the exact outcome for us short of doing all the work and profiling it afterwards.</p>
  
  <p>Anyway, thanks for all your excellent answers and help. Feel free to add anything else :).</p>