Skip to content

Instantly share code, notes, and snippets.

@pjmagee
Last active August 29, 2015 14:25
Show Gist options
  • Save pjmagee/a69d60ae2c3c847ed572 to your computer and use it in GitHub Desktop.
Save pjmagee/a69d60ae2c3c847ed572 to your computer and use it in GitHub Desktop.
var watch = new Stopwatch();
var htmlString = "<div class='isc-content-block' data-contentmanagerid='60dd19b0-e3ba-4629-935c-a2dd00e052b8' data-contentmanagername='Product: B456009805'>456009806</div>";
var regex = new Regex(">(.*)</.*>", RegexOptions.None); // NORMAL REGEX
// REGEX START
watch.Start();
// WARM UP JITTER
for(int i = 0; i < 10000000; i++)
{
regex.Match(htmlString);
// watch.Restart();
}
// END RESULTS
regex.Match(htmlString);
watch.ElapsedTicks.Dump("normal regex ticks");
watch.Reset();
// SUBSTRING START
watch.Start();
// WARM UP JITTER
for(int i = 0; i < 10000000; i++)
{
htmlString.Substring(htmlString.IndexOf(">") + 1).Replace("</div>", string.Empty);
// watch.Restart();
}
// END RESULTS
htmlString.Substring(htmlString.IndexOf(">") + 1).Replace("</div>", string.Empty);
watch.ElapsedTicks.Dump("substring ticks");
watch.Reset();
// REGEX COMPILED START
regex = new Regex(">(.*)</.*>", RegexOptions.Compiled); // COMPILE
watch.Start();
// WARM UP JITTER
for(int i = 0; i < 10000000; i++)
{
regex.Match(htmlString);
// watch.Restart();
}
regex.Match(htmlString);
watch.ElapsedTicks.Dump("compiled regex ticks");
watch.Reset();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment