Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to measure how long code takes to execute?

Tags:

performance

c#

I'm trying to determine which approach to removing a string is the fastest.

I simply get the start and end time and show the difference.

But the results are so varied, e.g. as shown below the same method can take from 60 ms to 231 ms.

What is a better method to get more accurate results?

alt text http://www.deviantsart.com/upload/1q4t3rl.png

using System; using System.Collections; using System.Collections.Generic;  namespace TestRemoveFast {     class Program     {         static void Main(string[] args)         {             for (int j = 0; j < 10; j++)             {                 string newone = "";                 List<string> tests = new List<string>();                 for (int i = 0; i < 100000; i++)                 {                     tests.Add("{http://company.com/Services/Types}ModifiedAt");                 }                  DateTime start = DateTime.Now;                 foreach (var test in tests)                 {                     //newone = ((System.Xml.Linq.XName)"{http://company.com/Services/Types}ModifiedAt").LocalName;                     newone = Clean(test);                 }                  Console.WriteLine(newone);                 DateTime end = DateTime.Now;                 TimeSpan duration = end - start;                 Console.WriteLine(duration.ToString());             }              Console.ReadLine();         }          static string Clean(string line)         {             int pos = line.LastIndexOf('}');             if (pos > 0)                 return line.Substring(pos + 1, line.Length - pos - 1);                 //return line.Substring(pos + 1);             else                 return line;         }     } } 
like image 463
Edward Tanguay Avatar asked Jan 15 '10 14:01

Edward Tanguay


People also ask

How is code execution time calculated?

We can calculate the execution time of the code using StartNew() and Stop() methods. StartNew() method comes under the Stopwatch class and it basically used to initialize a new Stopwatch instance.

How do I check the execution time in Visual Studio?

If milliseconds then in Visual Studio 2019 you can see the time between two breakpoints under Diagnostic Tools -> Events -> Duration (opens automatically in Debug mode, or use Ctrl + Alt + F2 ). Some notes: Make sure to measure performance of the Release configuration. Debug build performance is meaningless.


1 Answers

You should use System.Diagnostics.Stopwatch, and you might want to consider a large sample. For example, repeat this test something like 10,000 times and average the results. If you think about it scientifically, it makes sense. The larger the sample, the better. You can weed out a lot of edge cases this way and really see what the core performance is like.

Another thing to consider is that JIT compilation and object creation can definitely skew your results, so make sure that you start and stop your Stopwatch at the appropriate times, and call the methods you want to test at least once before you begin your tests. Try to segregate just the parts you want to test from the rest of your code as much as possible.

like image 148
Scott Anderson Avatar answered Sep 24 '22 03:09

Scott Anderson