user3559780
user3559780

Reputation: 363

Why does waiting for `clock` value to change by 5,000 units not result in a delay of five seconds?

So I am having 2 global variables assigned value with time(NULL) value. Now after 5 sec, I want to have my previous Time value remain unchanged and only current Time value gets modified. I want to display the time duration. So I have written the code but not getting the right result. On one compiler (DevC++) I get the proper result as timDrn == 5 but with an online compiler I get both variables at the same value and timDrn == 0. So am I going right or wrong? I want prevTim to remain unchanged and curTim value to be increased by 5 with timeDrn = 5.

My results with a local compiler (DevC++)

prevTim = 1585569873

curTim = 1585569873

prevTim = 1585569873

curTim = 1585569878

time duration = 5

My results with an online compiler

prevTim = 1585569915                                                                                                          

curTim = 1585569915                                                                                                           

prevTim = 1585569915                                                                                                          

curTim = 1585569915                                                                                                           

time duration = 0  
#include<stdio.h>
#include<time.h>

int prevTim ;
int curTim;
void delay(int number_of_seconds) 
{ 
    // Converting time into milli_seconds 
    int milli_seconds = 1000 * number_of_seconds; 

    // Storing start time 
    clock_t start_time = clock(); 

    // looping till required time is not achieved 
    while (clock() < start_time + milli_seconds) 
        ; 
} 
int main(void)
{
    int itr ,timDrn ;
    prevTim =time(NULL);
    curTim = prevTim; 

    printf("prevTim = %d \n",prevTim);  
    printf("curTim = %d \n",curTim);

    for(itr = 0 ;itr < 5 ;itr ++)
    {
        delay(1); //1 sec delay function
        curTim =time(NULL);
    }   

    timDrn =curTim - prevTim ;

    printf("prevTim = %d \n",prevTim);  
    printf("curTim = %d \n",curTim);
    printf("time duration = %d \n",timDrn);

    return 0;
}

Upvotes: 0

Views: 84

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 224311

The delay function incorrectly assumes clock returns time in milliseconds. The C standard does not specify this. Per C 2018 7.27.2.1 3:

To determine the time in seconds, the value returned by the clock function should be divided by the value of the macro CLOCKS_PER_SEC.

Delaying a program in this way is wasteful because it consumes processor time and is disrespectful in a submission to an online compiler. Although there is no standard C function to suspend program execution for a time, many systems provide a sleep function that will. (This is not wasteful because it requests the operating system to suspend program execution, so the processor can do other work or go into a low-power state.)

Upvotes: 0

liberforce
liberforce

Reputation: 11454

On the online compiler, the time at the end of the program is the same as when it started. So I guess your delay function probably returns immediately to avoid people putting very long delays and having their program run for ever on the website. Trust your local compiler. An online compiler is for convenience and basic testing, not for real work.

Also, delay doesn't exist in time.h, so I wonder how this compiles. Usually, one uses sleep.

Upvotes: 3

Related Questions