problems with time function !!
problems with time function !!
I have written a simple code using time function... here I am having
some undesired output.
I am copying the code for reference:
#include <stdio.h> #include <time.h> #include <sys/time.h> void main(void) { time_t t1; time_t t2; struct timeval tv; int i=0; t1 = time(;NULL); printf("*******T1 = %ld\n\n",t1); for(i;i<100000;i++); for(i;i>0;i--); t2 = time(NULL); printf("********T2 = %d\n\n\n",t2); tv.tv_sec = difftime(t2,t1); printf("***The time difference is %ld",tv.tv_sec); exit(); }
The output is:
********T1 = 1128099984 ********T2 = 1128099984 *** The time differene is : 0 \n
Therefore in both the cases time(NULL) is returning the same value. Do I need to use some other function to record the current time?
Pls suggest something !!! thanks, panther1 Oct 05, 8:37AM
you could also use "gettimeofday", it will return seconds AND microseconds.
robert
15 Nov 05, 12:40PM
There are many issues with your code....to start with... you should not use
void main(void)...it should be
int main(void)...secondly, make variable
i as long instead of int and try increasing the limit of in for loop to 100000000 or to even higher value ....you will notice a time difference
Login