title: Force compiler to not optimize side-effect-less statements
body: <p>I was reading some old game programming books and as some of you might know, back in that day it was usually faster to do bit hacks than do things the standard way. (Converting <code>float</code> to <code>int</code>, mask sign bit, convert back for absolute value, instead of just calling <code>fabs()</code>, for example)</p>
  <p>Nowadays is almost always better to just use the standard library math functions, since these tiny things are hardly the cause of most bottlenecks anyway.</p>
  <p>But I still want to do a comparison, just for curiosity's sake. So I want to make sure when I profile, I'm not getting skewed results. As such, I'd like to make sure the compiler does not optimize out statements that have no side effect, such as:</p>
  <pre><code>void float_to_int(float f)
  {
      int i = static_cast&lt;int&gt;(f); // has no side-effects
  }
  </code></pre>
  <p>Is there a way to do this? As far as I can tell, doing something like <code>i += 10</code> will still have no side-effect and as such won't solve the problem.</p>
  <p>The only thing I can think of is having a global variable, <code>int dummy;</code>, and after the cast doing something like <code>dummy += i</code>, so the value of <code>i</code> is used. But I feel like this dummy operation will get in the way of the results I want.</p>
  <p>I'm using Visual Studio 2008 / G++ (3.4.4).</p>
  <h3>Edit</h3>
  <p>To clarify, I would like to have all optimizations maxed out, to get good profile results. The problem is that with this the statements with no side-effect will be optimized out, hence the situation.</p>
  <h3>Edit Again</h3>
  <p>To clarify once more, read this: <em><strong>I'm not trying to micro-optimize this in some sort of production code.</strong></em></p>
  <p>We all know that the old tricks aren't very useful anymore, I'm merely curious <em>how</em> not useful they are. Just plain curiosity. Sure, life could go on without me knowing just how these old hacks perform against modern day CPU's, but it never hurts to know.</p>
  <p>So telling me &quot;these tricks aren't useful anymore, stop trying to micro-optimize blah blah&quot; is an answer completely missing the point. I <strong>know</strong> they aren't useful, I don't use them.</p>
  <p>Premature quoting of Knuth is the root of all annoyance.</p>