Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As John said numbers with a decimal place default to double. TomTom is wrong.</p> <p>I was curious to know if the compiler would just optimize the double to a const float (which I assumed would happen)... turns out it doesn't and the idea of the speed increase is actually legit... depending on how much you use it. In math-heavy application, you probably do want to use this trick.</p> <p>It must be the case that it is taking the stored float variable, casting it to a double, performing the math against the double (the number without the f), then casting it back to a float to store it again. That would explain the diference in calculation even though we're storing in floats each time.</p> <p>The code &amp; raw results: <a href="https://gist.github.com/1880400" rel="nofollow">https://gist.github.com/1880400</a></p> <p>Pulled out relevant benchmark on an iPad 1 in Debug profile (Release resulted in even more of a performance <em>increase</em> by using the f notation):</p> <pre> ------------ 10000000 total loops timeWithDoubles: 1.33593 sec timeWithFloats: 0.80924 sec Float speed up: 1.65x Difference in calculation: -0.000038 </pre> <p>Code:</p> <pre><code>int main (int argc, const char * argv[]) { for (unsigned int magnitude = 100; magnitude &lt; INT_MAX; magnitude *= 10) { runTest(magnitude); } return 0; } void runTest(int numIterations) { NSTimeInterval startTime = CFAbsoluteTimeGetCurrent(); float d = 1.2f; for (int i = 0; i &lt; numIterations; i++) { d += 1.8368383; d *= 0.976; } NSTimeInterval timeWithDoubles = CFAbsoluteTimeGetCurrent() - startTime; startTime = CFAbsoluteTimeGetCurrent(); float f = 1.2f; for (int i = 0; i &lt; numIterations; i++) { f += 1.8368383f; f *= 0.976f; } NSTimeInterval timeWithFloats = CFAbsoluteTimeGetCurrent() - startTime; printf("\n------------ %d total loops\n", numIterations); printf("timeWithDoubles: %2.5f sec\n", timeWithDoubles); printf("timeWithFloats: %2.5f sec\n", timeWithFloats); printf("Float speed up: %2.2fx\n", timeWithDoubles / timeWithFloats); printf("Difference in calculation: %f\n", d - f); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload