Reputation: 79
im actually doing a tool, where every ms counts, and i want to debug my program to see where is it slow and where its fast enough, cause somewhere it goes slowly, the program is like 600lines, is there any way to see execution time of EVERY function in my program? i could only find this method
Stopwatch stopWatch = Stopwatch.StartNew();
Thread.Sleep(10000);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
but i saw its not actually so accurate and i need it to be as accurate as possible. thanks!
Upvotes: 0
Views: 58
Reputation: 2797
I ran the following:
var stopWatch = Stopwatch.StartNew();
Thread.Sleep(1000);
stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
The output was 00:00:01.0002463
, please note that in this case, you might expect a second (reasonable expectation), but, it's slightly over a second. Why? Thread.Sleep()
isn't particularly accurate! Stopwatch is accurate enough to demonstrate that, and is in fact, generally accurate to nanosecond resolution.
That said, you're going about this the wrong way if you're trying to use stopwatches, you should instead profile your application with something like dotTrace and see what the hot spots are.
Upvotes: 3