Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The regex approach is probably the quickest to implement but not the quickest to run. I compared a simple regex solution to the following manual search code and found that the manual search code is ~2x-2.5x faster for large input strings and up to 4x faster for small strings:</p> <pre><code>static string Search(string expression) { int run = 0; for (int i = 0; i &lt; expression.Length; i++) { char c = expression[i]; if (Char.IsDigit(c)) run++; else if (run == 5) return expression.Substring(i - run, run); else run = 0; } return null; } const string pattern = @"\d{5}"; static string NotCached(string expression) { return Regex.Match(expression, pattern, RegexOptions.Compiled).Value; } static Regex regex = new Regex(pattern, RegexOptions.Compiled); static string Cached(string expression) { return regex.Match(expression).Value; } </code></pre> <p>Results for a ~50-char string with a 5-digit string in the middle, over 10^6 iterations, latency per call in microseconds (smaller number is faster):</p> <p>Simple search: 0.648396us</p> <p>Cached Regex: 2.1414645us</p> <p>Non-cached Regex: 3.070116us</p> <p>Results for a ~40K string with a 5-digit string in the middle over 10^4 iterations, latency per call in microseconds (smaller number is faster):</p> <p>Simple search: 423.801us</p> <p>Cached Regex: 1155.3948us</p> <p>Non-cached Regex: 1220.625us</p> <p>A little surprising: I would have expected Regex -- which is compiled to IL -- to be comparable to the manual search, at least for very large strings.</p>
    singulars
    1. This table or related slice is empty.
    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