473,473 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

TimeSpan accuracy

20 New Member
Hello,

I am using TimeSpan to calculate the time my program takes.
Here's the code

Expand|Select|Wrap|Line Numbers
  1. DateTime start = DateTime.Now;
  2.  
  3. //**my code here
  4.  
  5. DateTime end = DateTime.Now;
  6. span = end.Subtract(start);
  7.  
  8. ResultUserForm.Controls["lblTimer"].Text = string.Concat(span.Milliseconds.ToString(), "ms");

but the time I get in accuracy of 15ms.
I get either 15ms, or 31ms, or 46ms. And sometimes 0ms
I want it to show things like 8ms too, but it shows 0ms.

How to find a solution to this.
Aug 26 '09 #1
3 4860
cloud255
427 Recognized Expert Contributor
Well you can't really tell your program how long it should take (unless you use actual thread delays) so what is the problem? If the program never takes 8ms to execute then it never takes that amount of time. Also bear in mind that it might be 0 milliseconds but several hundredths of a second may have passed eg:

0:00:00:120 //h:mm:ss

TimeSpan.Milliseconds will never show how many seconds, minutes etc have passed...

As far as the accuracy, this depends on the clock speed at which your CPU is running (I think, my hardware knowledge is pretty limited), if that is the case then a faster CPU will make time measurement more accurate... but with modern machines and the scope of values you want to measure I cant image that the result is incorrect...
Aug 26 '09 #2
Plater
7,872 Recognized Expert Expert
You would want to use the .TotalMilliseconds property of a timespan then.
.Milliseconds returns JUST the millisecond portion of the time.
.TotalMilliseconds returns the entire structure in terms of milliseconds
Aug 26 '09 #3
PRR
750 Recognized Expert Contributor
Look into Stopwatch Class . Stopwatch class is used to measure execution time of a method...

MSDN
Expand|Select|Wrap|Line Numbers
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         Stopwatch stopWatch = new Stopwatch();
  6.         stopWatch.Start();
  7.         Thread.Sleep(10000);
  8.         stopWatch.Stop();
  9.         // Get the elapsed time as a TimeSpan value.
  10.         TimeSpan ts = stopWatch.Elapsed;
  11.  
  12.         // Format and display the TimeSpan value.
  13.         string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
  14.             ts.Hours, ts.Minutes, ts.Seconds,
  15.             ts.Milliseconds / 10);
  16.         Console.WriteLine(elapsedTime, "RunTime");
  17.     }
  18. }
  19.  
Aug 27 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jeff Shantz | last post by:
Hello, I'm developing a large statistics application for a call center. It often needs to calculate time spanning over months. For example, an agent's total talk time within 30 days. Since an...
3
by: Ivan A. | last post by:
Hi! Why I can't serialize TimeSpan structure with XmlSerializer? This is what I do: using System; using System.IO; using System.Xml; using System.Xml.Serialization;
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
2
by: DWalker | last post by:
In Visual Studio (Visual Basic) .NET 2002, I noticed that this: Dim Elapsed as DateTime = Now - Now gives a "compile time" error (error in the IDE), saying that the '-' operator is not...
10
by: Charles Law | last post by:
If I display a TimeSpan I get something like 00:05:17.6217891 when what I would like to see is 00:05:18 Is there an easy way to get this output? Try as I might I just can't find it.
2
by: ucasesoftware | last post by:
i translate a C# funtion to VB.NET and i have this : Shared Function isAllDay(ByVal ap As Appointment) As Boolean Return ap.DateBegin.TimeOfDay = TimeSpan.Zero AndAlso ap.DateEnd.TimeOfDay =...
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
5
by: style | last post by:
Hello In my custom configuration section, I'd like to specify an interval attribute which specifies an interval in milliseconds. I think it would be very comfortable to get this value directly...
4
by: Massimo | last post by:
Hi to All, i'm using C# in .NET 2.0 and i have a DataTable A with a column of type TimeSpan used to store HOUR info. I'm trying to filter my DataTable A selecting only rows that have the column...
0
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,...
0
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...
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.