473,714 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TimeSpan\Stopwa tch doesn't add up, why?

I am unable to add up two timespans as created by the
System.Diagnost ics.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.000220 4
Timer2: 00:00:01.999502 1
Sum: 00:00:01.431718 7 <-------

Thanks,
jeff
<Test()Public Sub StopwatchTest()
Dim myStopwatch As New Stopwatch
myStopwatch.Sta rt()
System.Threadin g.Thread.Sleep( 2000)
myStopwatch.Sto p()

Dim myStopwatch2 As New Stopwatch
myStopwatch2.St art()
System.Threadin g.Thread.Sleep( 2000)
myStopwatch2.St op()

Debug.WriteLine ("Timer1: " & myStopwatch.Ela psed.ToString)
Debug.WriteLine ("Timer2: " & myStopwatch2.El apsed.ToString)

Dim sumElapsedTime As New TimeSpan(myStop watch.ElapsedTi cks +
myStopwatch2.El apsedTicks)
Debug.WriteLine (" Sum: " & sumElapsedTime. ToString)
End Sub
Apr 1 '07 #1
3 2992
Jeff Jarrell wrote:
I am unable to add up two timespans as created by the
System.Diagnost ics.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.000220 4
Timer2: 00:00:01.999502 1
Sum: 00:00:01.431718 7 <-------

Thanks,
jeff
<Test()Public Sub StopwatchTest()
Dim myStopwatch As New Stopwatch
myStopwatch.Sta rt()
System.Threadin g.Thread.Sleep( 2000)
myStopwatch.Sto p()

Dim myStopwatch2 As New Stopwatch
myStopwatch2.St art()
System.Threadin g.Thread.Sleep( 2000)
myStopwatch2.St op()

Debug.WriteLine ("Timer1: " & myStopwatch.Ela psed.ToString)
Debug.WriteLine ("Timer2: " & myStopwatch2.El apsed.ToString)

Dim sumElapsedTime As New TimeSpan(myStop watch.ElapsedTi cks +
myStopwatch2.El apsedTicks)
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.Ela psed + myStopwatch2.El apsed

If you want to use the StopWatch ticks to create a TimeSpan, you have to
recalculate the value using the value from the StopWatch.Frequ ency 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 QueryPerformanc eCounter 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.Ela psed + myStopwatch2.El apsed

Hope this helps
--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"Jeff Jarrell" <jj************ @yahoo.comwrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>I am unable to add up two timespans as created by the
System.Diagnos tics.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.000220 4
Timer2: 00:00:01.999502 1
Sum: 00:00:01.431718 7 <-------

Thanks,
jeff
<Test()Public Sub StopwatchTest()
Dim myStopwatch As New Stopwatch
myStopwatch.Sta rt()
System.Threadin g.Thread.Sleep( 2000)
myStopwatch.Sto p()

Dim myStopwatch2 As New Stopwatch
myStopwatch2.St art()
System.Threadin g.Thread.Sleep( 2000)
myStopwatch2.St op()

Debug.WriteLine ("Timer1: " & myStopwatch.Ela psed.ToString)
Debug.WriteLine ("Timer2: " & myStopwatch2.El apsed.ToString)

Dim sumElapsedTime As New TimeSpan(myStop watch.ElapsedTi cks +
myStopwatch2.El apsedTicks)
Debug.WriteLine (" Sum: " & sumElapsedTime. ToString)
End Sub
Apr 1 '07 #3
That did the trick. Thanks.

"Göran Andersson" <gu***@guffa.co mwrote in message
news:uW******** ******@TK2MSFTN GP04.phx.gbl...
Jeff Jarrell wrote:
>I am unable to add up two timespans as created by the
System.Diagnos tics.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.000220 4
Timer2: 00:00:01.999502 1
Sum: 00:00:01.431718 7 <-------

Thanks,
jeff
<Test()Publi c Sub StopwatchTest()
Dim myStopwatch As New Stopwatch
myStopwatch.Sta rt()
System.Threadin g.Thread.Sleep( 2000)
myStopwatch.Sto p()

Dim myStopwatch2 As New Stopwatch
myStopwatch2.St art()
System.Threadin g.Thread.Sleep( 2000)
myStopwatch2.St op()

Debug.WriteLine ("Timer1: " & myStopwatch.Ela psed.ToString)
Debug.WriteLine ("Timer2: " & myStopwatch2.El apsed.ToString)

Dim sumElapsedTime As New TimeSpan(myStop watch.ElapsedTi cks +
myStopwatch2.E lapsedTicks)
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.Ela psed +
myStopwatch2.El apsed

If you want to use the StopWatch ticks to create a TimeSpan, you have to
recalculate the value using the value from the StopWatch.Frequ ency
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
4892
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 because then it didn't work in IE. How can I enable this javascipt form validation to work in Netscape? When I use netscape, none of the alert boxes appear. It submits the form without validating anything.
39
3325
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 (and whatever it contains), associated with class, 'a'. Somewhere else in, 'topdiv', there is a span or element (containing whatever), associated with class, 'b'.
4
3518
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 doesn't return any result. We have tried on our server and we always get a result. BUT, trying on the hosting server, many times the query doesn't return any result and doesn't get any error. Any idea ? does Union have any problem ? how to check if...
149
25154
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? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
43
2831
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 (calling tree) browsing that is supported in C++. I don't know how some could write a large scale object-oriented application w/o those. If you have overcome these limitations in C#, please share your thoughts and ideas. Thanks!
0
921
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 in, one different workstations each time, their IE 6.0 browser does not let them click on certain HTML links. The links are regular HTML to the browser, but when clicked nothing happens. No progress bar begins and the browser doesn't respond. ...
4
3407
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 work only on IE! My ASPX page works in FireFox. If the button doesn't work it's because of validators (and it worked with the same code in v1.0).
16
4920
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 Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
39
5860
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 ISO-8859-1 -t UTF-8 mydb.sql mydb_utf8.sql mysqlCREATE DATABASE mydb_utf8 CHARACTER SET utf8 COLLATE utf8_general_ci;
38
1788
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 = temp.ToUpper().IndexOf("friend")
0
8815
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8713
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9318
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9187
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7960
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6638
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5961
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.