Timing different variants of string.StartsWith


/ Published in: C#
Save to your folder(s)

Just an example of timing code


Copy this code and paste it in your HTML
  1. var a = "foo bar foo";
  2. var b = "foo";
  3.  
  4. int numTimes = 1000000;
  5.  
  6. var swOrdinal = Stopwatch.StartNew();
  7.  
  8. for (int i = 0; i < numTimes; i++)
  9. a.StartsWith(b, StringComparison.Ordinal);
  10.  
  11. swOrdinal.Stop();
  12.  
  13. Console.WriteLine("Ran {0:N0} ordinal iterations in {1}ms", numTimes, swOrdinal.Elapsed.TotalMilliseconds);
  14.  
  15. var swCulture = Stopwatch.StartNew();
  16.  
  17. for (int i = 0; i < numTimes; i++)
  18. a.StartsWith(b);
  19.  
  20. swCulture.Stop();
  21.  
  22. Console.WriteLine("Ran {0:N0} culture-sensitive iterations in {1}ms", numTimes, swCulture.Elapsed.TotalMilliseconds);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.