Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A nested for-loop is overkill, and in particular, the "remove" call itself represents yet another for-loop. All in all, your second optimized version has a worse time-complexity than the first solution, in particular when there are many hits.</p> <p>The fastest solution is likely to be the following:</p> <pre><code>foreach(var item in listItems) item.IsHit = false; foreach (int hitIndex in hits) listItems[hitIndex - 1].IsHit = true; </code></pre> <p>This avoids the inefficient nested for-loops, and it avoids the overhead of the delegate based <code>.ForEach</code> method (which is a fine method, but not in performance critical code). It involves setting IsHit slightly more frequently, but most property setters are trivial and thus this is probably not a bottleneck. A quick micro-benchmark serves as a fine sanity check in any case.</p> <p><strong>Only if <code>IsHit</code> is truly slow</strong>, the following will be quicker:</p> <pre><code>bool[] isHit = new bool[listItems.Count]; //default:false. //BitArray isHit = new BitArray(listItems.Count); //BitArray is potentially faster for very large lists. foreach (int hitIndex in hits) isHit [hitIndex - 1] = true; for(int i=0; i &lt; listItems.Count; i++) listItems[i].IsHit = isHit[i]; </code></pre> <p>Finally, consider using an array rather than a <code>List&lt;&gt;</code>. Arrays are generally faster if you can avoid needing the <code>List&lt;&gt;</code> type's insertion/removal methods.</p> <p>The <code>var</code> keyword is C# 3.5 but can be used in .NET 2.0 (new language features don't require newer library versions, in general - it's just that they're most useful with those newer libs). Of course, you know the type with which <code>List&lt;&gt;</code> is specialized, and can explicitly specify it.</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. 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