Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this code slower even if the function is inlined?
    text
    copied!<p>I have a method like this : </p> <pre><code>bool MyFunction(int&amp; i) { switch(m_step) { case 1: if (AComplexCondition) { i = m_i; return true; } case 2: // some code case 3: // some code } } </code></pre> <p>Since there are lots of case statements (more than 3) and the function is becoming large, I tried to extract the code in case 1 and put it in an inline function like this:</p> <pre><code>inline bool funct(int&amp; i) { if (AComplexCondition) { i = m_i; return true; } return false; } bool MyFunction(int&amp; i) { switch(m_step) { case 1: if (funct(i)) { return true; } case 2: // some code case 3: // some code } } </code></pre> <p>It seems this code is significantly slower than the original. <strong>I checked with -Winline and the function is inlined.</strong> Why is this code slower? I thought it would be equivalent. The only difference I see is there is one more conditional check in the second version, but I thought the compiler should be able to optimize it away. Right?</p> <p><strong>edit:</strong> Some peoples suggested that I should use gdb to stop over every assembly instructions in both versions to see the differences. I did this.</p> <p>The first version look like this :</p> <pre><code>mov callq (Call to AComplexCondition()) test je (doesn't jump) mov (i = m_i) movl (m_step = 1) </code></pre> <p>The second version, that is a bit slower seems simpler.</p> <pre><code>movl (m_step = 1) callq (Call to AComplexCondition()) test je (doesn't jump) mov (i = m_i) xchg %ax,%ax (This is a nop I think) </code></pre> <p>These two version seems to do the same thing, so I still don't know why the second version is still slower.</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