TimeSpan\Stopwatch doesn't add up, why?

J

Jeff Jarrell

I am unable to add up two timespans as created by the
System.Diagnostics.Stopwatch class.

Each stopwatch should be about two seconds and is appears ok, but when I try
and add them up it goes to heck. I need to keep sum totals and do avgs and
things.

Here is the result of the following NUnit test. I'd hope it to be a little
closer than that.. Aargh.
Timer1: 00:00:02.0002204
Timer2: 00:00:01.9995021
Sum: 00:00:01.4317187 <-------

Thanks,
jeff


<Test()> Public Sub StopwatchTest()
Dim myStopwatch As New Stopwatch
myStopwatch.Start()
System.Threading.Thread.Sleep(2000)
myStopwatch.Stop()

Dim myStopwatch2 As New Stopwatch
myStopwatch2.Start()
System.Threading.Thread.Sleep(2000)
myStopwatch2.Stop()

Debug.WriteLine("Timer1: " & myStopwatch.Elapsed.ToString)
Debug.WriteLine("Timer2: " & myStopwatch2.Elapsed.ToString)

Dim sumElapsedTime As New TimeSpan(myStopwatch.ElapsedTicks +
myStopwatch2.ElapsedTicks)
Debug.WriteLine(" Sum: " & sumElapsedTime.ToString)
End Sub
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Jeff said:
I am unable to add up two timespans as created by the
System.Diagnostics.Stopwatch class.

Each stopwatch should be about two seconds and is appears ok, but when I try
and add them up it goes to heck. I need to keep sum totals and do avgs and
things.

Here is the result of the following NUnit test. I'd hope it to be a little
closer than that.. Aargh.
Timer1: 00:00:02.0002204
Timer2: 00:00:01.9995021
Sum: 00:00:01.4317187 <-------

Thanks,
jeff


<Test()> Public Sub StopwatchTest()
Dim myStopwatch As New Stopwatch
myStopwatch.Start()
System.Threading.Thread.Sleep(2000)
myStopwatch.Stop()

Dim myStopwatch2 As New Stopwatch
myStopwatch2.Start()
System.Threading.Thread.Sleep(2000)
myStopwatch2.Stop()

Debug.WriteLine("Timer1: " & myStopwatch.Elapsed.ToString)
Debug.WriteLine("Timer2: " & myStopwatch2.Elapsed.ToString)

Dim sumElapsedTime As New TimeSpan(myStopwatch.ElapsedTicks +
myStopwatch2.ElapsedTicks)
Debug.WriteLine(" Sum: " & sumElapsedTime.ToString)
End Sub

You are mixing different types of ticks. StopWatch ticks and TimeSpan
ticks are not the same, so if you get the elapsed value in StopWatch
ticks and try to create a TimeSpan from it, the result will be wrong.

You can just add the TimeSpan values to get the sum:

Dim sumElapsedTime As TimeSpan = myStopwatch.Elapsed + myStopwatch2.Elapsed

If you want to use the StopWatch ticks to create a TimeSpan, you have to
recalculate the value using the value from the StopWatch.Frequency property.
 
C

Chris Taylor

Hi,

The problem is that the StopWatch if possible (it usually is) is using the
high performance QueryPerformanceCounter and the ticks do not translate to
the same tick interval as that expected by TimeSpan. The Elapsed property
does the required transformation to provide TimeSpan ticks so you should use
that.

sumElapsedTime = myStopwatch.Elapsed + myStopwatch2.Elapsed

Hope this helps
 
J

Jeff Jarrell

That did the trick. Thanks.

Göran Andersson said:
You are mixing different types of ticks. StopWatch ticks and TimeSpan
ticks are not the same, so if you get the elapsed value in StopWatch ticks
and try to create a TimeSpan from it, the result will be wrong.

You can just add the TimeSpan values to get the sum:

Dim sumElapsedTime As TimeSpan = myStopwatch.Elapsed +
myStopwatch2.Elapsed

If you want to use the StopWatch ticks to create a TimeSpan, you have to
recalculate the value using the value from the StopWatch.Frequency
property.
 
Top