title: Specific C++ code section increases code execution time
body: <p>I have written the following piece in order to test a function, which implements a logarithmic hysteretic quantizer.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;Eigen/Dense&gt;
  #include &lt;math.h&gt;
  
  double logarithmic_hysteretic_quantizer(double u, double u_prev, double delta, double u_min);
  
  int main(int argc, const char * argv[]) {
  
      Eigen::MatrixXd control_signal = Eigen::MatrixXd::Zero(7,1);
      double delta = 0.1765;
      double u_min = 0.01;
  
      double i = 0.0;
  
      while (i &gt;= -20.0) {
      
          std::cout &lt;&lt; &quot;FOR LOOP&quot; &lt;&lt; std::endl;
      
          control_signal &lt;&lt; logarithmic_hysteretic_quantizer(i, control_signal(0,0), delta, u_min),
                            logarithmic_hysteretic_quantizer(i, control_signal(1,0), delta, u_min),
                            logarithmic_hysteretic_quantizer(i, control_signal(2,0), delta, u_min),
                            logarithmic_hysteretic_quantizer(i, control_signal(3,0), delta, u_min),
                            logarithmic_hysteretic_quantizer(i, control_signal(4,0), delta, u_min),
                            logarithmic_hysteretic_quantizer(i, control_signal(5,0), delta, u_min), 
                            logarithmic_hysteretic_quantizer(i, control_signal(6,0), delta, u_min);
      
          std::cout &lt;&lt; control_signal &lt;&lt; std::endl;
          std::cout &lt;&lt; std::endl;
      
          i = i - 0.001;
      
          }
     return 0;
  }
  
  double logarithmic_hysteretic_quantizer(double u, double u_prev, double delta, double u_min) 
  {
  
      double u_i = 0.0;
      double u_i_next = 0.0;
      double p = (1.0-delta)/(1.0+delta);
  
      /* For Debugging Purposes */
      std::cout &lt;&lt; &quot;INSIDE&quot; &lt;&lt; std::endl;
      std::cout &lt;&lt; &quot;U is: &quot; &lt;&lt; u &lt;&lt; std::endl;
      std::cout &lt;&lt; &quot;U_PREV is: &quot; &lt;&lt; u_prev &lt;&lt; std::endl;
  
      if (u &lt; 0.0) {
          return -logarithmic_hysteretic_quantizer(-u, u_prev, delta, u_min);
      } else if (((u &gt;= 0.0) &amp;&amp; (u &lt;= u_min/(1.0+delta))) || ((u &lt; u_min) &amp;&amp; (u &gt; u_min/(1.0+delta)))) {
          std::cout &lt;&lt; &quot;DEAD ZONE&quot; &lt;&lt; std::endl;
          double w = 0.0;
          return w;
      
      } else {
      
          int i = 1;
      
          while(1) {
          
              u_i = pow(p,(1-i))*u_min;
              u_i_next = pow(p,(1-(i+1)))*u_min;
          
              if ( ((u &gt; u_i/(1.0+delta)) &amp;&amp; (u &lt;= u_i) &amp;&amp; (u_prev &gt;= u_i)) || ((u &lt; u_i/(1.0-delta)) &amp;&amp; (u_i &lt;= u) &amp;&amp; (u_prev &lt;= u_i)) ) {
              
                  return u_i;
              
              } else if ( ((u_i &lt; u) &amp;&amp; (u &lt;= u_i/(1.0-delta)) &amp;&amp; (u_prev &gt;= u_i*(1.0+delta))) || ((u_i/(1.0-delta) &lt;= u) &amp;&amp; (u &lt;= u_i_next) &amp;&amp; (u_prev &lt;= u_i*(1+delta))) ) {
              
                  return u_i*(1.0+delta);
              }
              i++;
          }
      }
  }
  </code></pre>
  <p>I am facing the following issue. When the <code>else if</code> statement (the one for the <code>DEAD ZONE</code>) is executed many times until the value gets out of the dead zone, the code executes somewhat slowly in the beggining while when this <code>else if</code> statement isn't executed many times (when for example <code>i = i - 1.0</code> instead of <code>i = i - 0.001</code>) the code is executed in a blink of an eye. I tried using breakpoints at this section in order to check if a particular instruction takes some time but this doesn't seem to be the case. Profiling the code also gave me a general idea of how much time the <code>logarithmic_hysteretic_quantizer()</code> function takes but this doesn't really help me to understand what's going on. Could anyone give me insight of what's going on at this section of the code ?</p>
  <p>P.S.: I am running the code at Xcode 12.4.</p>