Skip to content

Instantly share code, notes, and snippets.

@hagronnestad
Created January 16, 2017 12:51
Show Gist options
  • Save hagronnestad/84001c6d0a2870aee36206900a398fab to your computer and use it in GitHub Desktop.
Save hagronnestad/84001c6d0a2870aee36206900a398fab to your computer and use it in GitHub Desktop.
var longString = "This is a long string.";
var iterations = 1000000;
var timer = new Stopwatch();
"\nTake()".Dump();
timer.Start();
for (int i = 0; i < iterations; i++) {
var newString = new string(longString.Take(10).ToArray());
if (i == 0) newString.Dump();
}
timer.Stop();
timer.Elapsed.TotalMilliseconds.Dump();
timer.Reset();
"\nSubstring()".Dump();
timer.Start();
for (int i = 0; i < iterations; i++) {
var newString = longString.Length < 11 ? longString : longString.Substring(0, 10);
if (i == 0) newString.Dump();
}
timer.Stop();
timer.Elapsed.TotalMilliseconds.Dump();
timer.Reset();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment