View Full Version : calculate elapsed seconds
George2
04-12-2008, 08:46 AM
Hello everyone,
What is the most convenient way to check whether elapsed time from some time point to another time point is larger than 2 seconds?
Currently, I am using TimeSpan, and I am using stupid method to check hour/minute/second one by one to decide. Are there any quick way to check?
thanks in advance,
George
guesst
04-12-2008, 09:13 AM
timer. Check this:
#include <time.h>
#include <stdio.h>
int main () {
time_t start, end;
double length;
timer(&start);
printf ("Start counting and press any key.");
getchar ();
timer(&end);
length = difftime(end, start);
printf ("%.0f seconds.", length);
}
Magos
04-12-2008, 09:39 AM
I'm using a System.DateTime which has been sufficient for my needs so far.
System.DateTime TimePoint1 = System.DateTime.Now;
//Do some timeconsuming stuff here
System.DateTime TimePoint2 = System.DateTime.Now;
long MillisecondsBetweenTimePoints = (TimePoint2.Ticks - TimePoint1.Ticks) / 10000;
if(MillisecondsBetweenTimePoints > 2000)
{
//Do some stuff here if the timeconsuming stuff took more than 2 seconds
}
George2
04-13-2008, 12:26 AM
It is not C# code, guesst. :-)
timer. Check this:
#include <time.h>
#include <stdio.h>
int main () {
time_t start, end;
double length;
timer(&start);
printf ("Start counting and press any key.");
getchar ();
timer(&end);
length = difftime(end, start);
printf ("%.0f seconds.", length);
}
Cool, Magos!
I'm using a System.DateTime which has been sufficient for my needs so far.
System.DateTime TimePoint1 = System.DateTime.Now;
//Do some timeconsuming stuff here
System.DateTime TimePoint2 = System.DateTime.Now;
long MillisecondsBetweenTimePoints = (TimePoint2.Ticks - TimePoint1.Ticks) / 10000;
if(MillisecondsBetweenTimePoints > 2000)
{
//Do some stuff here if the timeconsuming stuff took more than 2 seconds
}
regards,
George
Raven Arkadon
04-19-2008, 10:58 AM
The most convenient way i found was using System.Diagnostics.Stopwatch.
(In fact, i was just writing a stopwatch class when i figured out there was already one)
using System.Diagnostics;
Stopwatch sw = new Stopwatch();
sw.Start();
while(sw.ElapsedMilliseconds < 2000)
{
if(sw.ElapsedMilliseconds > 1000) { }
}
George2
04-20-2008, 01:21 AM
Thanks Raven,
What are the pros and cons comparing DateTime/TimeSpan with Stopwatch?
The most convenient way i found was using System.Diagnostics.Stopwatch.
(In fact, i was just writing a stopwatch class when i figured out there was already one)
using System.Diagnostics;
Stopwatch sw = new Stopwatch();
sw.Start();
while(sw.ElapsedMilliseconds < 2000)
{
if(sw.ElapsedMilliseconds > 1000) { }
}
regards,
George
Raven Arkadon
04-20-2008, 04:45 AM
Stopwatch has 2 static properties:
.) IsHighResolution and
.) Frequency - the number of ticks - so even if you calculate in ticks you shouldn't assume that its constant (e.g. just divide by 10000), but optain that value from from some api function (note that these ticks may only be valid for the stopwatch object, since other objects might not use the high resolution timer).
(Unfortunately i don't know if high resolution timers have a negative performance inpact)
For example, in my case IsHighResolution is true, so the Frequency the stopwatch is running is 3192270000 ticks/sec. (woot, just figured that out, thats high)
Also, the difference between two DateTimes is a TimeSpan (sometimes things are quite logic ;)
So DateTime is a point in time, and TimeSpan is the difference between 2 timestamps.
You could also write:
DateTime start = DateTime.Now;
// some code;
DateTime end = DateTime.Now;
// Note that DateTime has an overloaded - operator which returns a TimeSpan:
TimeSpan elapsed = end - start;
Btw, concerning your initial question:
Why don't you use the .TotalXXX methods of the TimeSpan? Actually you could have figured that out with IntelliSense ;)
E.g. elapsed.TotalSeconds > 2.0, or elapsed.TotalMilliseconds > 2000.0
(note: the TotalXXX methods return the result as double, to avoid overflows)
Oh the advantage of stopwatch: Better timing resolution, and "more object oriented" usage - to the 1/10 of a millisecond (the other method only rounds to the nearest 10 ms)
George2
04-20-2008, 05:57 AM
Thanks Raven,
I have two more comments,
1.
About IsHighResolution, it is readonly, who is responsible to set its value?
http://msdn2.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution.aspx
2.
Do you mean the frequency value for different H/W may be different even in the same .NET version?
Stopwatch has 2 static properties:
.) IsHighResolution and
.) Frequency - the number of ticks - so even if you calculate in ticks you shouldn't assume that its constant (e.g. just divide by 10000), but optain that value from from some api function (note that these ticks may only be valid for the stopwatch object, since other objects might not use the high resolution timer).
(Unfortunately i don't know if high resolution timers have a negative performance inpact)
For example, in my case IsHighResolution is true, so the Frequency the stopwatch is running is 3192270000 ticks/sec. (woot, just figured that out, thats high)
Also, the difference between two DateTimes is a TimeSpan (sometimes things are quite logic ;)
So DateTime is a point in time, and TimeSpan is the difference between 2 timestamps.
You could also write:
DateTime start = DateTime.Now;
// some code;
DateTime end = DateTime.Now;
// Note that DateTime has an overloaded - operator which returns a TimeSpan:
TimeSpan elapsed = end - start;
Btw, concerning your initial question:
Why don't you use the .TotalXXX methods of the TimeSpan? Actually you could have figured that out with IntelliSense ;)
E.g. elapsed.TotalSeconds > 2.0, or elapsed.TotalMilliseconds > 2000.0
(note: the TotalXXX methods return the result as double, to avoid overflows)
Oh the advantage of stopwatch: Better timing resolution, and "more object oriented" usage - to the 1/10 of a millisecond (the other method only rounds to the nearest 10 ms)
regards,
George
Raven Arkadon
04-20-2008, 07:15 AM
About IsHighResolution, it is readonly, who is responsible to set its value?
The system. (Well actually the Stopwatch queries the system for a high resolution timer - and if it doesn't find one, i guess it resorts to the DateTime method).
Do you mean the frequency value for different H/W may be different even in the same .NET version?
No, not for DateTime (The stopwatch frequence may vary though), but its better style to use constants provided api (if there are any) ;)
E.g. In C there is the constant CLOCKS_PER_SEC which is usually 1000000, but there might be exotic systems where it has a different value.
George2
04-20-2008, 07:18 AM
Thanks Raven,
Your reply is clear. What do you think Stopwatch is "more object oriented" usage? Compared with what do you have this conclusion? :-)
The system. (Well actually the Stopwatch queries the system for a high resolution timer - and if it doesn't find one, i guess it resorts to the DateTime method).
No, not for DateTime (The stopwatch frequence may vary though), but its better style to use constants provided api (if there are any) ;)
E.g. In C there is the constant CLOCKS_PER_SEC which is usually 1000000, but there might be exotic systems where it has a different value.
regards,
George
Raven Arkadon
04-20-2008, 07:32 AM
>> What do you think Stopwatch is "more object oriented" usage? Compared with what do you have this conclusion? :-)
By "more object oriented" i mean that the measuring is packed into a class. You just call Start(), Elapsed(Milli)Seconds and End() which seems more natural than defining a variable start, and then retrieve the current time in substract start from it again.
George2
04-21-2008, 01:18 AM
Thanks Raven,
Got your ponits.
>> What do you think Stopwatch is "more object oriented" usage? Compared with what do you have this conclusion? :-)
By "more object oriented" i mean that the measuring is packed into a class. You just call Start(), Elapsed(Milli)Seconds and End() which seems more natural than defining a variable start, and then retrieve the current time in substract start from it again.
regards,
George
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.