Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Does the compiler take care of enhancing the 'second' code so its performance would be as good as the 'first' code?</p> </blockquote> <p>No, it cannot.</p> <ol> <li><p>It doesn't know that the boolean expression will not change between iterations of the loop. It's <em>possible</em> for the code to not return the same value each time, so it is forced to perform the check in each iteration.</p></li> <li><p>It's also possible that the boolean expression could have side effects. In this case it doesn't, but there's no way for the compiler to know that. It's important that such side effects would be performed in order to meet the specs, so it needs to execute the check in each iteration.</p></li> </ol> <p>So, the next question you need to ask is, in a case such as this, is it <em>important</em> to perform the optimization that you've mentioned? In any situation I can imagine for the exact code you showed, probably not. The check is simply going to be so fast that it's almost certainly not going to be a bottleneck. If there are performance problems there are almost certainly bigger fish.</p> <p>That said, with only a few changes to the example it can be made to matter. If the boolean expression itself is computationally expensive (i.e. it is the result of a database call, a web service call, some expensive CPU computation, etc.) then it could be a performance optimization that matters. Another case to consider is what would happen if the boolean expression had side effects. What if it was a <code>MoveNext</code> call on an <code>IEnumerator</code>? If it was important that it only be executed exactly once because you don't want the side effects to happen N times then that makes this a <em>very</em> important issue.</p> <p>There are several possible solutions in such a case.</p> <p>The easiest is most likely to just compute the boolean expression once and then store it in a variable:</p> <pre><code>bool someValue = ComputeComplexBooleanValue(); foreach(var item in collection) { if(someValue) doStuff(item); else doOtherStuff(item); } </code></pre> <p>If you want to execute the boolean value 0-1 times (i.e. avoid calling it even once in the event that the collection is empty) then we can use <code>Lazy</code> to lazily compute the value, but ensure it's still only computed at most one time:</p> <pre><code>var someValue = new Lazy&lt;bool&gt;(() =&gt; ComputeComplexBooleanValue()); foreach (var item in collection) { if (someValue.Value) doStuff(item); else doOtherStuff(item); } </code></pre>
    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. 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.
    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