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.
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?!?!Originally Posted by Codeplug
...
from time.h
What did you say??Code:/* 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!
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.
Last edited by Hunter2; 10-06-2004 at 10:46 PM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Actually, the standard states that time_t is an arithmetic type.
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):
Originally Posted by Prelude
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
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.
Last edited by xErath; 10-07-2004 at 07:33 AM.
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!
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT