Hello Mohd,
> [EMAIL PROTECTED] asked:
> I am trying to read time to 1/100th of a sec. in Linux using C.
> Most of the time functions (from the man) are good to a sec.
> The gettimeofday() promises to tell time down to microseconds.
> That is better than what I wanted.
>
> I tried the following progam to test my understanding of struct.
> It seems to work if I just declare one pair struct variable the way
> it is in the program. If I declace two pairs struct variables say
> like this
>
> struct timeval *t1;
> struct timezone *z1;
>
Hmm. This looks dangerous - t1 and z1 are actually only
pointers to structs. You should declare the structs and
then reference them using the & whenever a pointer is
needed. See code below.
The manpage also says that you can do away with the
timezone argument - pass a NULL pointer instead.
> The program follows:
>
include <sys/time.h>
#include <unistd.h>
#include <math.h>
int main(){
struct timeval t1, t2;
int k;
double w;
gettimeofday( &t1, NULL );
printf("start: sec = %10d usec = %6d\n", t1.tv_sec, t1.tv_usec);
for(k=1; k<=400000; k++){
w = tan(k);
}
gettimeofday(&t2, NULL );
printf("stop : sec = %10d usec = %6d\n", t2.tv_sec, t2.tv_usec);
if( t2.tv_usec - t1.tv_usec < 0 ){
t2.tv_usec += 1000000;
t2.tv_sec--;
}
printf("diff : sec = %10d usec = %6d\n",
t2.tv_sec - t1.tv_sec, t2.tv_usec - t1.tv_usec );
return 0;
}
HTH,
Thomas