I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
Index » Programming » C/C++ »

problems with time function !!

panther_vip\′s Photo
30 Sep 05, 4:24PM
(2 replies)
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 &#58; 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, panther

robsc\′s Photo
1 Oct 05, 8:37AM
you could also use "gettimeofday", it will return seconds AND microseconds.

robert
resistantgnome\′s Photo
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
Currently you need to be logged in to leave a message.