Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The two constructs are not identical. In particular, in the first case you don't need any jumps (on x86, you can use cmp and setle and add, instead of having to use cmp and jb and (if you don't jump) add. Not jumping is faster than jumping on pretty much every modern architecture.</p> <p>So, if you have code that looks like</p> <pre><code>if (a &lt; b) x += 1 </code></pre> <p>where you <em>may</em> add or you <em>may</em> jump instead, vs.</p> <pre><code>x += (a &lt; b) </code></pre> <p>(which only makes sense in C/C++ where 1 = true and 0 = false), the latter tends to be faster as it can be turned into more compact assembly code. In Scala/Java, you can't do this, but you <em>can</em> do</p> <pre><code>x += if (a &lt; b) 1 else 0 </code></pre> <p>which a smart JVM should recognize is the same as x += (a &lt; b), which has a jump-free machine code translation, which is usually faster than jumping. An even smarter JVM would recognize that</p> <pre><code>if (a &lt; b) x += 1 </code></pre> <p>is the same yet again (because adding zero doesn't do anything).</p> <p>C/C++ compilers routinely perform optimizations like this. Being unable to apply any of these optimizations was not a mark in the JIT compiler's favor; apparently it can as of 1.7, but only partially (i.e. it doesn't recognize that adding zero is the same as a conditional adding one, but it does at least convert <code>x += if (a&lt;b) 1 else 0</code> into fast machine code).</p> <p>Now, none of this has anything to do with tail recursion or while loops per se. With tail recursion it's more natural to write the <code>if (a &lt; b) 1 else 0</code> form, but you can do either; and with while loops you can also do either. It just so happened that you picked one form for tail recursion and the other for the while loop, making it look like recursion vs. looping was the change instead of the two different ways to do the conditionals.</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.
    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.
 

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