Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default time precision of DateTime?

I did a small winform program for data transferring in Visual Studio, and I used a method to provide the transferring time duration. After the transferring being done, the program will return a dialog window to show the time.

But here I don't know what is the time precision or the resolution of the timer, how can it be such a precision, even micro second?

var startTime = DateTime.Now;
this.transferdata();
var endTime = DateTime.Now;
var timeElapsed = endTime.Subtract(startTime);

enter image description here

when I saw the definition of class DateTime, there is only a precision in milisecond. can anybody tell me why there is such a high resolution timer in the visual studio 2012? Or there is related to the operating system?

like image 910
SeaJay Avatar asked Aug 16 '14 21:08

SeaJay


People also ask

What is precision in DateTime?

The datetime precision specifies number of digits after the decimal dot and can be any integer number from 0 to 6. If no precision is specified it is assumed to be 0, for backward compatibility reasons. A datetime precision can be specified wherever a type name is used.

How precise is DateTime now?

From MSDN you'll find that DateTime. Now has an approximate resolution of 10 milliseconds on all NT operating systems. The actual precision is hardware dependent.

How accurate is DateTime C#?

First, lets take a look at precision: The DateTime type is basically just a 64 bit integer that counts “ticks”. One tick is 100 nanoseconds (or 0.0001 milliseconds) long (MSDN). So DateTime 's precision can be up to 0.0001 milliseconds.

How do I get milliseconds from DateTime?

To display the millisecond component of a DateTime valueParse(String) or DateTimeOffset. Parse(String) method. To extract the string representation of a time's millisecond component, call the date and time value's DateTime.


1 Answers

The precision of the clock depends on the operating system. The system clock ticks a certain number of times per second, and you can only measure whole ticks.

You can test the resolution for a specific computer using code like this:

DateTime t1 = DateTime.Now;
DateTime t2;
while ((t2 = DateTime.Now) == t1) ;
Console.WriteLine(t2 - t1);

On my computer the result is 00:00:00.0156253, which means that the system clock ticks 64 times per second.

(Note: The DateTime type also has ticks, but that is not the same as the system clock ticks. A DateTime tick is 1/10000000 second.)

To measure time more precisely, you should use the Stopwatch class. Its resolution also depends on the system, but is much higher than the system clock. You can get the resolution from the Stopwatch.Frequency property, and on my computer it returns 2143566 which is a tad more than 64...

Start a stopwatch before the work and stop it after, then get the elapsed time:

Stopwatch time = Stopwatch.StartNew();
this.transferdata();
time.Stop();
TimeSpan timeElapsed = time.Elapsed;

That will return the time in the resolution that the TimeSpan type can handle, e.g. 1/10000000 second. You can also calculate the time from the number of ticks:

double timeElapsed = (double)s.ElapsedTicks / (double)Stopwatch.Frequency;
like image 114
Guffa Avatar answered Oct 29 '22 08:10

Guffa