Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Regarding speed, this would be at least as fast as choice 2:</p> <pre><code>bSize = 128 &lt;&lt; (choice == 'y' || choice == 'Y'); </code></pre> <p>Whether <em>that</em> would be faster than choice 1 is not immediately obvious to me. However, for tuned performance on an unknown platform, I think that I like the suggested variant on choice 2. The reason is that, at the hardware level, choice 2 (original or variant) does not involve reloading the program counter, but invokes a relatively straightforward <em>shift-register</em> operation, involving relatively few transistors. (Actually, if you want to get really technical about it, I am given to understand that the shift is probably accomplished by <em>multiplexing.</em> To detail that would be too much for the present format, but the point is that the output of <code>(choice == 'y' || choice == 'Y')</code> is effectively piped straight to one of the multiplexer's control lines. Anyway, it's really fast.)</p> <p>Regarding whether one can safely use the evaluated condition in the manner suggested, ISO/IEC 9899:1999 (E), sect. 6.5.14.3, guarantees that one can safely do this. It reads, "The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int."</p> <p>(@PaulR rightly observes that electronic-theoretical considerations like the ones this answer offers are not decisive. One must profile actual code on an actual platform to tell for sure which is faster. Nor is this a mere quibble on @PaulR's part. It is all well to argue that choice 2 <em>would</em> be faster, but this does not mean that is <em>is</em> faster. Depending on the CPU in use, branch-predicting or other hardware could promote choice 1, nor would I be extremely surprised if it did.)</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