I'm comparing execution time of different Redis clients. I'm following the advice of several Stack Overflow answers, like this one (in regards to using stopwatch for a simple timer). Notice my results in the image below. The Visual Studio execution time is 926ms but the timer value is 16,815ms. Stopwatch results are consistently higher (by quite a lot). What am I doing wrong?
Asked
Active
Viewed 372 times
2
-
@HansPassant, I believe the first line shown is when VS timer is initiated. It's also when debugger will provide interval for its elapsed time. – mad moe Feb 22 '20 at 14:37
1 Answers
0
The Stopwatch
measures elapsed time between Start
and Stop
by counting timer ticks in the underlying timer mechanism. Having said that means if you use F10
to step over statements then also Stopwatch
is running in background.
Let's perform a test to compare both time with below test to get better understanding. Suppose breakpoint
is on 1st line and on last time. Press F5
when execution is paused at 1st break-point and compare time shown by debugger and Stopwatch. Those would be comparable.
Stopwatch stopwatch = Stopwatch.StartNew(); //Breakpoint
Print(); //Have a Thread.Sleep(1000);
stopwatch.Stop();
int count = 10; //Breakpoint
In my case Stopwatch
showed elapsed time as 1002ms
and debugger showed <1005ms
.
[
Please note that times would significantly differ if you step over each line (one at a time). Stopwatch will show significantly greater time as elapsed time would be more.

MKR
- 19,739
- 4
- 23
- 33
-
1MKR, first, thank you for the detailed explanation. I'm not stepping thru the code but using F5 at first break point (as you suggest)...so code runs thru the second breakpoint. Notice the great discrepancy as my question shows. When I run the same loop with ServiceStack the stopwatch and debugger times are nearly identical. When I use StackExchange Redis client, I get the great discrepancies. It seems like StackExchange client impacts the stopwatch. – mad moe Feb 29 '20 at 02:22
-
@madmoe That's a good assessment. Need to find root-cause. So what is your observation? Does it actually take around 16 seconds (which is shown by StopWatch) or StopWatch is reporting false value? – MKR Feb 29 '20 at 08:59