Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy in this example using floats makes me go 2x slower than with doubles?
    primarykey
    data
    text
    <p>I've been doing some profiling lately and I've encountered one case which is driving me nuts. The following is a piece of unsafe C# code which basically copies a source sample buffer to a target buffer with a different sample rate. As it is now, it takes up ~0.17% of the total processing time per frame. What I don't get is that if I use floats instead of doubles, the processing time will raise to 0.38%. Could someone please explain what's going on here?</p> <p><b>Fast version (~17%)</b></p> <pre><code>double rateIncr = ... double readOffset = ... double offsetIncr = ... float v = ... // volume // Source and target buffers. float* src = ... float* tgt = ... for( var c = 0; c &lt; chunkCount; ++c) { for( var s = 0; s &lt; chunkSampleSize; ++s ) { // Source sample var iReadOffset = (int)readOffset; // Interpolate factor var k = (float)readOffset - iReadOffset; // Linearly interpolate 2 contiguous samples and write result to target. *tgt++ += (src[ iReadOffset ] * (1f - k) + src[ iReadOffset + 1 ] * k) * v; // Increment source offset. readOffset += offsetIncr; } // Increment sample rate offsetIncr += rateIncr; } </code></pre> <p><b>Slow version (~38%)</b></p> <pre><code>float rateIncr = ... float readOffset = ... float offsetIncr = ... float v = ... // volume // Source and target buffers. float* src = ... float* tgt = ... for( var c = 0; c &lt; chunkCount; ++c) { for( var s = 0; s &lt; chunkSampleSize; ++s ) { var iReadOffset = (int)readOffset; // The cast to float is removed var k = readOffset - iReadOffset; *tgt++ += (src[ iReadOffset ] * (1f - k) + src[ iReadOffset + 1 ] * k) * v; readOffset += offsetIncr; } offsetIncr += rateIncr; } </code></pre> <p><b>Odd version(~22%)</b></p> <pre><code>float rateIncr = ... float readOffset = ... float offsetIncr = ... float v = ... // volume // Source and target buffers. float* src = ... float* tgt = ... for( var c = 0; c &lt; chunkCount; ++c) { for( var s = 0; s &lt; chunkSampleSize; ++s ) { var iReadOffset = (int)readOffset; var k = readOffset - iReadOffset; // By just placing this test it goes down from 38% to 22%, // and the condition is NEVER met. if( (k != 0) &amp;&amp; Math.Abs( k ) &lt; 1e-38 ) { Console.WriteLine( "Denormalized float?" ); } *tgt++ += (src[ iReadOffset ] * (1f - k) + src[ iReadOffset + 1 ] * k) * v; readOffset += offsetIncr; } offsetIncr += rateIncr; } </code></pre> <p>All I know by now is that I know nothing</p>
    singulars
    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.
 

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