I am extremely confused with this stopWatch.Elapsed property. It shows the time value in this format
I want to know what should I add in the end of this output. Is it ms (01:20:17.0550410ms) or just s (01:20:17.0550410s)?
And further more if I want to take only the msportion of this output and do some calculation with it which value should I take, is it .0550410 or 55.041? My questions might sound silly but I'm really confused!
please help.
The unit shown is "fractional parts of a second", to the 7th decimal place - i.e. to the 10-millionth of a second.
Since a millisecond is 0.001 seconds, your number of milliseconds is 55.041.
Now, as everyone else said, use stopWatch.Elapsed.TotalMilliseconds (to get all of the milliseconds) or stopWatch.Elapsed.Milliseconds (to get all of the milliseconds less than 1 second).
I am extremely confused with this stopWatch.Elapsed property. It shows the time value in this format
I'll begin at the beginning, just to make sure everything is understood.
Microsoft .Net framework designers have decided that all variables that store information derive from either a Class or a Struct(ture). Both of these base types have some defaults methods. One of these Methods for a ValueType (which I'll discuss below) is .ToString(). When you call the ToString() method on a Windows Runtime structure, it provides the default behavior for value types that don’t override ToString().
I will assuming you are talking about the System.Diagnostics.Stopwatch class. The documentation states:
A Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals. In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.
Before we begin looking at the Properties of the Stopwatch, we need to understand that in order for the Stopwatch to work, there needs to be a way to store a Magnitude of Time (information). A Magnitude of Time in this case a numerical representation of the difference between two points in time; in this case the when the Stopwatch Starts and when the Stopwatch Ends. Microsoft .Net has created a structure called TimeSpan to store this value.
Taking a look at the documentation for the property Stopwatch.Elapsed it is of the type TimeSpan.
Since your screenshot appears to be in a console application, I'll assume the code (which should always be provided but isn't) is the following:
Console.Writeline("Took Time: " + stopWatch.Elapsed.ToString());
This code converts the TimeSpan into a string using the TimeSpan's .ToString() override:
So not passing a value to .ToString() is a null value which defaults to ("c") It's in the TimeSpan Format Strings:
So using your example:
01:20:17.0550410s
It should be obvious that 01 is hours, 20 is minutes, 17 is seconds and .0550410 are tenths of seconds (decisecond). Adding any string values at the end will most likely make no sense because all the numbers are of different time durations. The only way I think it would make sense if you wanted be more specific is to change it to:
01h 20m 17.0550410s
or
01h 20m 17s 055.0410ms
According to the documentation for TimeSpan:
A TimeSpan value can be represented as [-]d.hh:mm:ss.ff, where [...] ss is seconds, and ff is fractions of a second
So in your case, it is 17.0550410 seconds.
Elasped is a TimeSpan, what you're showing is the representation from writing it to the console (Same as calling .ToString() on any non string object), that representation is in hours:minutes:seconds.fraction of second. If you want to show this in a specific amount there are properties for this on the TimeSpan so instead of doing
Console.WriteLine(stopWatch.Elapsed);
You can do
Console.WriteLine(stopWatch.Elapsed.TotalMilliseconds);
A full example to clarify
Console.WriteLine(
"The timer ran for " + stopWatch.Elapsed.Hours + " Hours, "
+ stopWatch.Elapsed.Minutes + " Minutes and "
+ stopWatch.Elapsed.Seconds + ". this amounts to a total of "
+ stopWatch.Elapsed.TotalMilliseconds + " ms" );
It looks like you are just using the default .ToString() method of the Stopwatch.Elapsed property, which actually is a TimeSpan object. You can absolutely control the string formatting of this TimeSpan, as well as use numeric components of it for mathematical operations (rounding, adding, etc.). Please read this: Stopwatch.Elapsed Property
A quick search of 'c# stopwatch' returns the MSDN documentation showing that Stopwatch.Elapsed is a TimeSpan. Then a search on 'c# TimeSpan' returns the MSDN documentation showing that TimeSpan.ToString() returns a string with the following format: [-][d.]hh:mm:ss[.fffffff]. And that's just using google. Next time try doing some research before asking your question.
Assuming that you are interested in StopWatch.Elapsed, then...
In situations like this I find the documentation is often quite helpful.
It states that StopWatch.Elapsed returns
A read-only TimeSpan representing the total elapsed time measured by the current instance.
The documentation for TimeSpan states its many available properties, one of which is Milliseconds and another is TotalMilliseconds. According to the docs:
Milliseconds - Gets the milliseconds component of the time interval represented by the current TimeSpan structure.
whereas
TotalMilliseconds - Gets the value of the current TimeSpan structure expressed in whole and fractional milliseconds.
Other properties allow you to check Days, Hours, Minutes, Seconds etc. Please use the documentation as it saves everyone a lot of time and effort.
Related
I currently use a solution for getting a higher resolution timestamp in C# by taking a start time using DateTime.UtcNow and then using a Stopwatch to add ticks to it as time goes by. I came across Stopwatch.GetTimestamp() as a potential alternative or even better solution, but I cannot find reliable information on exactly what this function returns.
Best source of info seems to be this.
GetTimestamp() returns machine-dependent ticks which can be converted into seconds by dividing by the stopwatch frequency. If I do this, I get a value that appears to be a UTC UNIX timestamp which is exactly what I'm after - but I haven't seen anything that states that this is what I should expect from it.
One clue from MSDN states that:
If the Stopwatch class uses a high-resolution performance counter,
GetTimestamp returns the current value of that counter. If the
Stopwatch class uses the system timer, GetTimestamp returns the
current DateTime.Ticks property of the DateTime.Now instance.
Looking then at DateTime.Ticks, we then see:
The value of this property represents the number of 100-nanosecond
intervals that have elapsed since 12:00:00 midnight, January 1, 0001
(0:00:00 UTC on January 1, 0001, in the Gregorian calendar), which
represents DateTime.MinValue.
I'm therefore not clear how simply dividing some machine-dependent tick-count by the frequency can get me a UNIX 1970+ timestamp? Is it possible that if a high performance timer is not available on the target platform that I might get year 0001-based timestamp instead? Or maybe something else entirely, again depending on the available hi-res timer?
Can you describe your use case? If you're interested in extra precision, I don't see how you could possibly get it by starting out with DateTime.UtcNow, and then, separately, calling Stopwatch.Start() -- if you add Stopwatch.Elapsed to DateTime.UtcNow, the value is going to be inaccurate, because you have no way of knowing how long after the DateTime.UtcNow call that the stopwatch actually started. If you start the stopwatch first, you have the same problem in reverse.
Generally speaking, in .NET 4.6, there is a ToUnixTimeMilliseconds call on DateTimeOffset that may be helpful (e.g. DateTimeOffset.UtcNow.ToUnixTimeMilliseconds())
First of all, sorry about my ignorance and my awful english skills, i work to improve them.So here goes my question:
I want use DateTime.Ticks (instead Guid.NewGuid) in order to calculate an identifier and a question is being raised to me. In my current culture we have 2 days on the year when we change the official time: in octuber we add an hour and in april we remove it.
how does it affect to the ticks value? how ticks value is calulated? As far as i understand based on https://msdn.microsoft.com/en-us/library/system.datetime.ticks%28v=vs.110%29.aspx it seems it is not able to be a repeat value because based on the text (...)It does not include the number of ticks that are attributable to leap seconds(...) .
there could be repeated ticks?, (maybe other question would be how long a tick lasts, depends on the computer? )
If i'm not wrong it cant be repeated.
Moreover, Maybe there could be a lot of stuff i misunderstand so i'm really sorry again...
Even without DST changes, you can observe DateTime.Now.Ticks returning the same value multiple times, due to the granularity of the system clock.
But with DST changing, if you use DateTime.Now, you will indeed see the same values repeating for one hour per year. The Ticks property is just "the number of ticks since the DateTime epoch, in whatever kind of value is represented". (A DateTime value can be one of three "kinds" - universal, local, or unspecified. It's all a bit of a mess.)
If you use DateTime.UtcNow you shouldn't see that if your system clock only moves forward... but it's entirely possible for system clocks to be changed, either manually or in an automated way, to correct them for drift. Basically, it's not a good source of uniqueness on its own.
(In terms of the length of a tick - the tick in DateTime is always 100ns. That's not true for Stopwatch, where you need to use the Frequency property to find out how long a tick is, or use the Elapsed property to just find the elapsed time as a TimeSpan.)
In C#
DateTime dateAndTime = DateTime.Now;
gives the current date and time. I need only the current time. If I use string, it is possible like:
string time=DateTime.Now.ToString("hh:mm:ss");
Is it possible to get only the time portion of DateTime without going through a string?
You can get the current time in a TimeSpan by accessing TimeOfDay, like this:
TimeSpan time = DateTime.Now.TimeOfDay;
Now time will represent the amount of time that passed since midnight.
There is no out-of-the-box type that holds just a time. You could use TimeSpan, and even the .NET framework does at parts (for example DateTime.TimeOfDay), however I think that TimeSpan is really serving a different purpose.
TimeSpan, to quote MSDN, simply "measures a time interval". That could easily be longer than the hours, minutes, seconds, etc. that make up a day (i.e. 24 hours in sum). And indeed the TimeSpan structure provides for that, having properties like Days, for example.
Thus, I think TimeSpan is not a very good fit to represent the time of day (which I assume you mean when saying "current time").
That brings us to another problem. What exactly is the "current time"? As I said, I assume that you mean the current time as in "the current time of day". But current time could also mean the current (elapsed) time since some particular point in time in the past.
Granted, all that can get pretty theoretic or even rhetoric and does not really help you.
I would just use DateTime. Where you actually care about the "time" value, just only use the time portion (like you have shown, you known about, with your ToString example). Although, depending on what you need the time for, you might resort to the DateTime.TimeOfDay property instead of simply formatting it as a string (unless of course that is what you need).
Finally, you could also resort to third party libraries like Node Time, that do provide types for time only (like LocalTime).
string time = string.Format("{0:hh-mm-ss-tt}", DateTime.Now);
What should be the best way to calculate the time diff which is accurate upto the level of Microseconds. currently I am doing as follows:
((TimeSpan)(DateTime.Now - _perfClock)).TotalMilliseconds
Note: perfClock is DateTime (set prior to task)
Which is suppose to give accuracy upto Milliseconds, but in my case it is showing values ends with "000". like 8000,9000 etc...
This is forcing me to think that is just converting seconds to Milliseconds somewhere, instead of calculating diff in Milliseconds. (Possibly I am wrong somewhere in code above).
But what should be the recommended mechanism for accurate Time Diff calculation?
-Sumeet
The issue is not with TimeSpan, that is accurate down to ticks, which is 100 nanoseconds.
The issue is you are using DateTime.Now for your timer.
DateTime.Now is accurate to about 16ms i believe. as mentioned by V4Vendetta, you want to use Stopwatch if you need "high resolution" results. Stopwatch can provide you with ticks (long) or TimeSpan. use Timespan for easy manipulation (in your case, add/subtract).
Note also that Stopwatch provides a .IsHighResolution property, to see if you have a better accuracy than Datetime.Now (it's always true on PC iirc)
I don't know the context in which you are measuring time but it would be good to start of with Stopwatch and check your results.
Also worth a read Precise Measurement
Have you tried:
TimeSpan diff = DateTime.Now.Subtract(_perfClock);
I need to format the day time using QueryPerformanceCounter Win32 API.
The format, is: HH:mm:ss.ffffff , containing hours minuts seconds and microseconds.
I need to use THIS function, because another process (written in C) is using this function and the purpose is using the same function in both places.
Thanks
The System.Diagnostics.Stopwatch class uses QueryPerformanceCounter(), saves you from having to P/Invoke it.
You should not use QueryPerformanceCounter to determine time of day. It can only be used to determine an elapsed interval with a very high resolution as it returns the number of ticks that passed since the computer was last restarted.
As such, at best, you may only determine how many hours, minutes, and seconds have passed since a previous reading of QueryPerformanceCounter which must not have happened too long in the past.
In order to convert from ticks to seconds you need to determine the frequency (using QueryPerformanceFrequency) of the ticks on the computer you're running the QueryPerformanceCounter function and then divide your reading by that frequency:
// obtain frequency
long freq;
QueryPerformanceFrequency(freq);
// then obtain your first reading
long start_count;
long end_count;
QueryPerformanceCounter(start_count)
// .. do some work
// obatin your second reading
QueryPerformanceCounter(end_count);
// calculate time elapsed
long milliseconds_elapsed = (long)(((double)(end_count - start_count) / freq) * 1000);
// from here on you can format milliseconds_elapsed any way you need to
An alternative to the above example would be to use the TimeSpan structure available in .Net which has a constructor that takes ticks like so:
// then obtain your first reading
long start_count;
long end_count;
QueryPerformanceCounter(start_count)
// .. do some work
// obatin your second reading
QueryPerformanceCounter(end_count);
TimeSpan time_elapsed = new TimeSpan(end_count - start_count);
Console.WriteLine("Time Elapsed: " + time_elapsed.ToString());
Can use :
1) The System.Diagnostics.Stopwatch class uses QueryPerformanceCounter(), saves you from having to P/Invoke it.
2) Can use directly by importing from the Win32 dll . [DLLImport(Win32)] and the name ofthe function
Possibly I misunderstand the question, as for me none of the previous answers are relevant at all.
I had the problem (which sent me here): Given a value from QueryPerformanceCounter, because something out of my control specifies timestamps using that function, how can I convert these values to a normal date / time?
I figured that QueryPerformanceCounter returns the number of seconds since the system booted, multiplied (and extended in resolution) depending on QueryPerformanceFrequency.
Thus, the most simple solution is to get the current date/time, subtract the amount of seconds returned by QueryPerformanceCounter/QueryPerformanceFrequency, and then add the values you like to format as time of day.