using difftime for loop parameter

Go To StackoverFlow.com

0

I am trying to create a loop function based on the time. After the iteration, it will print every 60 second "60 second passed". But this code result me in couple of "60 second passed" while actually me watch does not even showing 1 minute already.. I tried below, but I am expecting it to show me this information, but it does not (only the first couple of lines of iteration. afterwards not..)

Can anyone help in this matter? Thank you

#include <stdio.h>
#include <time.h>

int main()
{
time_t start,stop;
start = time(NULL);
time(&start);
int iteration, i;
    for (iteration = 1; iteration <= 500; iteration++) {
            for (i = 0; i <= 50; i++) {
                     printf("looping while waiting 60 second..\n");

             }
            stop = time(NULL);
            int diff = difftime(start, stop);
            if (diff % 60 == 0) {
                     printf("60 second passed..");}
            }

    return 1;
}
2012-04-04 16:53
by xambo
BTW--this is an example of "busy waiting" and is generally considered poor practice if it is at all avoidable. Your OS should provide a way to pause for a specified amount of time (for instance sleep (3)) - dmckee 2012-04-04 17:11


2

difftime and the following code are likely being executed multiple times before even one second has passed. As a result, difftime will return a value < 1, which is truncated to 0 by your implicit cast. And of course 0 % 60 == 0.

EDIT:

You might consider something like:

start = time(NULL);
for(; /* some condition that takes forever to meet */;) {
    // do stuff that apparently takes forever.
    stop = time(NULL);
    double diff = difftime(stop, start);
    if (diff >= 60) {
        printf("60 seconds passed...");
        start = time(NULL);
    }
}

Also note that in your example code, start and stop should be flipped in the call to difftime.

2012-04-04 17:02
by jpm
thank you for this remark. I tried to change it to double, but actually the % only can compare between integer (hence the same problem like above.. or? - xambo 2012-04-04 17:05
The problem isn't really that it's being coerced to int, it's that the test is happening several times a second. So, if this code actually ran for more than a minute (which I very much doubt), every time it hit a minute mark (including 0), it would spit out the message several times - jpm 2012-04-04 17:09
I will later on change the "iteration" towards big number. for now I just need to get the basic idea of checking how long the function works before doing something else (without sleep - xambo 2012-04-04 17:16
@xambo Please see the edited answer - jpm 2012-04-04 17:18


2

It is very likely that difftime is equal to 0. If you want to wait for 60 seconds you should consider using functions:

  • usleep on linux
  • sleep on windows
2012-04-04 17:09
by Jarosław Gomułka
Gomulka : yes, I am aware about sleep(). but the thing is I have to do some stuffs later with number of i, that can cause a problem with sleep. to make short, I just need to print something every 60 second, and change parameter of the loop.. i thought with difftime will help. no - xambo 2012-04-04 17:12
Ok, I didn't know that. If changing condition to diff > 0 && diff % 60 == 0 doesn't help you, than I don't know answer to this problem;/ - Jarosław Gomułka 2012-04-04 17:17
Ads