473,414 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 2944
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes...
39
by: Mark Johnson | last post by:
It doesn't seem possible. But would the following also seem a violation of the general notions behind css? You have a DIV, say asociated with class, 'topdiv'. Inside of that you have an anchor...
4
by: Bob Bedford | last post by:
We have no access to a mysql NG on my provider's server, so we ask here: We have a long query (long in text) with a UNION between 2 select. We have been informed that some times the query...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
43
by: Zeng | last post by:
It's so messy w/o the "friend" relationship. Does anyone know why it was not supported in C#. It's almost about as bad as it doesn't support the inheritance hierarchy and method reference...
0
by: Christopher Beltran | last post by:
This may not be an asp.net problem but one of our customers is running into this with our asp.net application. They log into our web application which is hosted on their servers. After logging...
4
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
38
by: tshad | last post by:
In VS 2008, why doesn't ToString require "()". If I have Option Strict On on why can I do both: selectedIndex.ToString() selectedIndex.ToString or sQuery =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.