Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>TestOne</code> will be faster than <code>TestTwo</code>, because you're not creating a new regular expression for every loop iteration.</p> <p>This has two benefits:</p> <ul> <li>The time used to parse and construct the objects for the regex is only done once, instead of <code>mc.Count</code> times</li> <li>Less pressure on garbage collection since fewer objects are constructed.</li> </ul> <p>However, I would go one step further. If you're always going to return that same regular expression, and you're concerned about speed, I would cache that regex object in a static field.</p> <p>For instance, you might consider this:</p> <pre><code>private static Regex _FindNumber; public static Regex FindNumber() { if (_FindNumber == null) _FindNumber = new Regex(@"\d+", RegexOptions.IgnoreCase | RegexOptions.Compiled); return _FindNumber; } </code></pre> <p>This would create just one object, total, and keep it around.</p> <p>However, and here's my real answer.</p> <p>To actually know which one is going to be the fastest, you're going to have to measure your code, optionally with my variant thrown in for good measure, and then decide. Never decide on optimizations without hard data, you might end up spending time rewriting code, which can introduce new bugs, which will need fixing, which you will spend more time on, only to eek out another 1% of performance.</p> <p>The big optimizations are done algorithmically, like changing the type of sorting algorithm, and then only afterwards, if necessary, you move on to local optimizations like loop tuning.</p> <p>Having said that, I would at least avoid constructing the object in the loop, that's just common sense.</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.
 

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