Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeSpan into Microseconds?

hello again my question if it is possible to print microsencods using TimesSpan in C#.

i have the next example:

DateTime startTime = DateTime.Now;
..../some code
DateTime endTime = DateTime.Now;
TimeSpan totalTimeTaken = endTime.Subtract(startTime);
Label3.Text=(totalTimeTaken.Milliseconds).ToString();

this code allows me to display in milliseconds but i need to display Microseconds is there a way to change it to Microseconds or do I have to use something else?

have to edit my question: my goal is to measure the time that is spent between the 2 DateTime, since it is very few lines of code the milliseconds just wont cut it, thats the reason i need the microseconds.

like image 826
JUAN Avatar asked Jul 18 '12 18:07

JUAN


People also ask

What is TimeSpan type in C#?

C# TimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds. C# TimeSpan is used to compare two C# DateTime objects to find the difference between two dates.

What is the default value for TimeSpan in C#?

Time(TimeSpan?): Defines the current selection of time interval. The default value is null.


2 Answers

The highest resolution you will find will be via the Stopwatch class. If your CPU supports a high resolution timer, the Stopwatch class will use it. You can check this via the IsHighResolution and/or Frequency properties.

This is the class you should be using for performance measurements, not DateTime.

EDIT: I just learned something new about DateTime. From the docs...

Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks that would be added by leap seconds). For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.

However, the resolution of DateTime.Now is still about 15ms, so it won't work (which makes the documentation misleading at best...) Use the Stopwatch.

like image 132
Ed S. Avatar answered Oct 24 '22 16:10

Ed S.


Use System.Diagnostics.Stopwatch if you need a high level precision.

From the code sample if looks like you are trying to time something. DateTime is a poor structure for that. Please see Eric Lippert's article "Precision and Accuracy of DateTime" for more information.

Now, the question “how much time has elapsed from start to finish?” is a completely different question than “what time is it right now?” If the question you want to ask is about how long some operation took, and you want a high-precision, high-accuracy answer, then use the StopWatch class. It really does have nanosecond precision and accuracy that is close to its precision.

like image 43
asawyer Avatar answered Oct 24 '22 17:10

asawyer