hi could anyone let me know an efficient way to measure time?
e.g. i want my program to stay in a loop for exactly 20 seconds, what would be the best way to do that?
thanks.
Printable View
hi could anyone let me know an efficient way to measure time?
e.g. i want my program to stay in a loop for exactly 20 seconds, what would be the best way to do that?
thanks.
Before starting the loop, get and save the current system time. This is your start time. Then, everytime through the loop, get the current system time and compare it to the start time. I won't tell you what methods to use because every system and OS have different ways of getting and representing the system time.
PK
If you need more help, state your OS and compiler and if you really need "exactly" 20 seconds or just really close to it.
gg
Code:#include<time.h>
...
time_t end = time(0)+20;
while(time(0)<end){
//your loop
}
>>+20
There are no arithmetic properties defined for type time_t. You should use difftime().
gg
ah?!?! :eek: :eek:Quote:
Originally Posted by Codeplug
...
from time.h
What did you say?? :o :o :oCode:/* Define the implementation defined time type */
#ifndef _TIME_T_DEFINED
typedef long time_t; /* time value */
#define _TIME_T_DEFINED /* avoid multiple def's of time_t */
#endif
Busted! :D
Once again, I quote Prelude: "My compiler does this" != "The standard says this". I can't tell you what the standard says about the subject, but quoting your compiler's source code is no proof that you are correct... For all you know, on another compiler/system, time_t may be a struct or union, or even a pointer. While in the former cases you'd have a compiler error to catch you, in the latter case you would end up with a nasty bug that would be incredibly difficult to find, especially if you assume that time_t is just a long and therefore eliminate it as a potential problem.
**EDIT**
>>/* Define the implementation defined time type */
This strongly supports my argument. It means that any compiler-writer is free to implement the time_t however they wish, as long as it functions in the same way that the standard defines.
Actually, the standard states that time_t is an arithmetic type.
Quote:
typedef a-type time_t;
The type is the arithmetic type a-type of an object that you declare to hold the value returned by time. The value represents calendar time.
http://cboard.cprogramming.com/showthread.php?t=49580
Draw your own conclusions.
gg
another standard holy-war, but I think Prelude answered it best (as she usually does):
Quote:
Originally Posted by Prelude
time
Syntax
#include <time.h>
time_t time(time_t *t);
Description
If t is not NULL, the current time is stored in *t.
Return Value
The current time is returned.
Portability
ANSI, POSIX
Example
printf("Time is %d\n", time(0));
I sincerely doubt that anyone you be smart enough to write a time funtion, with the standard prototipe that doesn't manipulate time_t as long. NOTE that time(...) return the time. structs can't be returned em C, although this is C++, and I'm not aware of any time.h for C++ or <time> that might have a struct declaration for time_t.
If you don't care about portability or adhering to the standard, then do whatever works.
And you can return structs in C and C++.
gg
http://www.cs.tut.fi/~leopold/Boar/AnsiLib/time.html
<time.h> provides the user tools for manipulating dates and time.
(...)
clock_t: An arithmetic type representing times.
time_t: An arithmetic type representing times.
struct tm: A datatype capable of holding the components of a calendar time. It has at least the following members that the user of the library may access:
int tm_sec: Seconds after the minute (0...59)
int tm_min: Minutes after the hour (0..59)
int tm_hour: Hours since midnight (0..23)
int tm_mday: Day of the month (1..31)
int tm_mon: Months since January (0..11)
int tm_year: Years since 1900
int tm_wday: Days since Sunday (0..6)
int tm_yday: Days since January 1st (0..365)
int tm_isdst: Daylight Saving Time flag. This flag will be positive if Daylight Savings Time is in effect, and zero if not. If the information is not available, tm_isdst is negative.
(...)
Who said structs can't be returned in C++??
On DJGPP <sys\djtypes.h>
typedef unsigned int time_t;
All I find are arithmetic definitions of time_t.
Please post a link with a time_t definition diferent from the integer one.
either way, running a loop and checking with ever iteration eats your processor time like you wouldn't believe, and you can't do anything outside the loop while it's running (unless you spawn another thread), but since it seems like you want to work inside the loop anyway, code on!
>>All I find...
What you find has absolutely nothing to do with what the standard says.
Here's how one would continue this argument:
- Buy a copy of the standard.
- Read it.
- Assert your interpretations of the standard that support your argument.
Otherwise, read the links provided in this thread and drawn your own conclusions. Here's mine: "If you don't care about portability or adhering to the standard, then do whatever works".
gg
Nothing to do with the standard??? Ok then maybe I've been posting MS specific code or something without knowing...
But you still haven't presented that link to justify your opinion... Do that and I'll believe you.
And I have a copy of the standard (pdf though). Let me quote it:
typedef long time_t;
I understand perfectly what you're saying but, I've never eared or readed anything about time_t not being arithmetic. There are non arithmetic types like struct tm.
http://www.infosys.utas.edu.au/info/...C/CStdLib.html
http://refcards.com/refcards/c/c-refcard-letter.pdf
>> Let me quote it:
How bout a real qoute will some section numbers etc.
Then what you'll want to do is explain how the quote supports your argument.
gg
I've done that, you didn't...
Try looking in the time.h section
Does the standard really define the implementation of the standard library functions? I was under the impression that it only defined the behaviour - in which case it is likely that the time.h that you are referring to (I'm assuming that you're looking at a header file and not an explicit statement of how the time_t must be defined) is only an example of how time.h might be implemented, and is not binding for those who wish to implement the standard in their own compilers.
Well, I usually try to teach folks how to fish, but some just need to be spoon fed…..here's how you quote the standard:
This is from C99 but applies equally to C89.Code:From: ISO/IEC 9899:1999 (E)
7.23 Date and time <time.h>
7.23.1 Components of time
...
3 The types declared are size_t (described in 7.17);
clock_t and time_t which are arithmetic types capable of representing
times; and struct tm which holds the components of a calendar time,
called the broken-down time.
...
7.23.2.4 The time function
Synopsis
1 #include <time.h>
time_t time(time_t *timer);
Description
2 The time function determines the current calendar time. The encoding of
the value is unspecified.
Returns
3 The time function returns the implementation’s best approximation to
the current calendar time. The value (time_t)(-1) is returned if the
calendar time is not available. If timer is not a null pointer, the
return value is also assigned to the object it points to.
There's a big difference between arithmetic properties (my original post) and an arithmetic type. The standard defines time_t as arithmetic type so that a comparison to (time_t)(-1) can be made. More importantly, the encoding of the time value is not defined. Meaning it could be seconds, micro-seconds, nano-seconds, or none of the above. The encoding doesn't even have to be in any unit of time, it could be a binary encoded decimal (BCD) of date/time values (adding 20 to a BCD doesn't make any sense).
Under ISO C, time_t is an abstract type for which the only valid arithmetic operation is comparison to (time_t)(-1).
As a side note, under the Single Unix Specification (the current POSIX standard), the time_t value returned by the time() function is defined to be in units of seconds. This is the most common implementation I've seen over the years.
So, (once again) if you're not concerned with portability of source code under ISO C, simply follow the documentation that came with your standard C library implementation. As a contributor to these forums, I'm simply following the convention of pointing out what is standard and what isn't standard (as far as I understand it).
I prefer "boo-yaa" over "busted" ;)
gg