Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I took another look at this and realised that the best way to speed it up would be to rewrite</p> <pre><code> for (size_t i = 0; i &lt; kRuns; ++i) { c = c + (a + b) * 0.5f; } </code></pre> <p>as</p> <pre><code>c = c + kRuns * ( a + b ) * 0.5f </code></pre> <p>which takes no time at all.</p> <p>When I hand code it using simple arrays, my optimizing compiler obviously does this for the 'loop' runs a million times in a time too short to measure.</p> <pre><code> float a[3], b[3], c[3]; a[0] = 1.0f, a[1] = 2.0f, a[2] = 3.0f; b[0] = 2.0f, b[1] = 3.0f, b[2] = 1.0f; c[0] = 6.0f, c[1] = 4.0f, c[2] = 5.0f; for (size_t i = 0; i &lt; KRUNS; ++i) { c[0] = c[0] + ( a[0] + b[0] ) * 0.5; c[1] = c[1] + ( a[1] + b[1] ) * 0.5; c[2] = c[2] + ( a[2] + b[2] ) * 0.5; } </code></pre> <p>Doesn't yours?</p> <p>Using the ublas library prevents the optimizer doing its thing. Running this code</p> <pre><code> #define KRUNS 1000000 typedef boost::numeric::ublas::vector&lt;float, boost::numeric::ublas::bounded_array&lt;float, 3&gt; &gt; MYVECTOR3; MYVECTOR3 a(3), b(3), c(3); a[0] = 1.0f, a[1] = 2.0f, a[2] = 3.0f; b[0] = 2.0f, b[1] = 3.0f, b[2] = 1.0f; c[0] = 6.0f, c[1] = 4.0f, c[2] = 5.0f; for (size_t i = 0; i &lt; KRUNS; ++i) { noalias(c) = c + (a + b) * 0.5f; } </code></pre> <p>takes 63 milliseconds. I cannot imagine why it should take 9400 milliseconds for you, no matter how slow your machine. I have ask again: are you sure you have switched on optimization and are linking to release libraries?</p>
 

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