469,324 Members | 1,414 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,324 developers. It's quick & easy.

TimeSpan\Stopwatch doesn't add up, why?

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
Apr 1 '07 #1
3 2689
Jeff Jarrell wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Apr 1 '07 #2
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
--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"Jeff Jarrell" <jj************@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>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
Apr 1 '07 #3
That did the trick. Thanks.

"Göran Andersson" <gu***@guffa.comwrote in message
news:uW**************@TK2MSFTNGP04.phx.gbl...
Jeff Jarrell wrote:
>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.

--
Göran Andersson
_____
http://www.guffa.com

Apr 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

39 posts views Thread by Mark Johnson | last post: by
4 posts views Thread by Bob Bedford | last post: by
149 posts views Thread by Christopher Benson-Manica | last post: by
reply views Thread by Christopher Beltran | last post: by
4 posts views Thread by bbp | last post: by
38 posts views Thread by tshad | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.