Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The obligatory Regex answer:</p> <pre><code>Regex re = new Regex(@"(\w){50,}"); re.IsMatch(input); </code></pre> <p>PS: While I tend to stay away from Regex, I feel in this case a Regex is simpler and faster.</p> <p><strong>Edit after @jimmy_keen's experiment</strong></p> <p>@jimmy_keen - Thanks for that.</p> <p>I removed the group, since it was not really needed and the time taken was much lesser for Regex, below is the output for compiled:</p> <p>Changed Regex:</p> <pre><code>Regex regex = new Regex(@"\w{50,}", RegexOptions.Compiled); </code></pre> <p>Output:</p> <blockquote> <p>LINQ [noLongWords], 1000000 iterations. Result = False: 725 ms</p> <p>LINQ [oneLongWordAtEnd], 1000000 iterations. Result = True: 760 ms</p> <p>LINQ [oneLongWordAtBegining], 1000000 iterations. Result = True: 651 ms</p> <p>LINQ [manyLongWordsEverywhere], 1000000 iterations. Result = True: 2107 ms</p> <p>Regex [noLongWords], 1000000 iterations. Result = False: 2773 ms</p> <p>Regex [oneLongWordAtEnd], 1000000 iterations. Result = True: 7217 ms</p> <p>Regex [oneLongWordAtBegining], 1000000 iterations. Result = True: 3279 ms</p> <p>Regex [manyLongWordsEverywhere], 1000000 iterations. Result = True: 3283 ms</p> <p>DONE</p> </blockquote> <p>The regex being slower than LINQ in this situation was sort of surprising for, but maybe not, because I have been burnt by regexes many times. They are a great tool, but very heavy. In this case, I was thinking the regex would be able to come slightly ahead of LINQ, but that is not to be.</p> <p>A slightly more complex, but similar regex outperforms LINQ in many long words case:</p> <pre><code>Regex regex = new Regex(@"[A-Za-z0-9]{50,}", RegexOptions.Compiled); </code></pre> <blockquote> <p>LINQ [noLongWords], 1000000 iterations. Result = False: 611 ms</p> <p>LINQ [oneLongWordAtEnd], 1000000 iterations. Result = True: 642 ms </p> <p>LINQ [oneLongWordAtBegining], 1000000 iterations. Result = True: 549 ms </p> <p>LINQ [manyLongWordsEverywhere], 1000000 iterations. Result = True: 1914 ms</p> <p>Regex [noLongWords], 1000000 iterations. Result = False: 2085 ms</p> <p>Regex [oneLongWordAtEnd], 1000000 iterations. Result = True: 4147 ms</p> <p>Regex [oneLongWordAtBegining], 1000000 iterations. Result = True: 1569 ms</p> <p>Regex [manyLongWordsEverywhere], 1000000 iterations. Result = True: 1580 ms </p> <p>DONE</p> </blockquote> <p><strong>But I still believe that the simplicty of the regex and that the performance difference starts to be seen at 100,000 iterations or more, regex is still the best solution here</strong></p>
 

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