Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can C#'s string.IndexOf perform so fast, 10 times faster than ordinary for loop find?
    primarykey
    data
    text
    <p>I have a very long string (<strong>60MB</strong> in size) in which I need to find how many pairs of '&lt;' and '>' are in there.</p> <hr> <p>I have first tried my own way:</p> <pre><code> char pre = '!'; int match1 = 0; for (int j = 0; j &lt; html.Length; j++) { char c = html[j]; if (pre == '&lt;' &amp;&amp; c == '&gt;') //find a match { pre = '!'; match1++; } else if (pre == '!' &amp;&amp; c == '&lt;') pre = '&lt;'; } </code></pre> <p>The above code runs on my string for roughly <strong>1000 ms</strong>.</p> <hr> <p>Then I tried using <code>string.IndexOf</code></p> <pre><code> int match2 = 0; int index = -1; do { index = html.IndexOf('&lt;', index + 1); if (index != -1) // find a match { index = html.IndexOf('&gt;', index + 1); if (index != -1) match2++; } } while (index != -1); </code></pre> <p>The above code runs for only around <strong>150 ms</strong>. </p> <hr> <p>I am wondering what is the magic that makes <code>string.IndexOf</code> runs <strong>so fast</strong>?</p> <p>Anyone can inspire me?</p> <hr> <p><strong>Edit</strong></p> <p>Ok, according to @BrokenGlass's answer. I modified my code in the way that they don't check the pairing, instead, they check how many '&lt;' in the string.</p> <hr> <pre><code> for (int j = 0; j &lt; html.Length; j++) { char c = html[j]; if (c == '&gt;') { match1++; } } </code></pre> <p>the above code runs for around <strong>760 ms</strong>.</p> <hr> <p>Using <code>IndexOf</code></p> <pre><code> int index = -1; do { index = html.IndexOf('&lt;', index + 1); if (index != -1) { match2++; } } while (index != -1); </code></pre> <p>The above code runs for about <strong>132 ms</strong>. <strong>still very very fast.</strong> </p> <hr> <p><strong>Edit 2</strong></p> <p>After read @Jeffrey Sax comment, I realised that I was running in VS with Debug mode.</p> <p>Then I built and ran in release mode, ok, <code>IndexOf</code> is still faster, but not that faster any more.</p> <p>Here is the results:</p> <p>For the pairing count: <strong>207ms VS 144ms</strong></p> <p>For the normal one char count: <strong>141ms VS 111ms</strong>.</p> <p>My own codes' performance was really improved. </p> <hr> <p><strong>Lesson learned: when you do the benchmark stuff, do it in release mode!</strong></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.
 

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