The delay() Function

Most C programmers know the sleep() function, which pauses program execution for a given number of seconds. Seconds are a vast chunk of time, especially in a computer where things happen quickly. So a desire exists for a function that delays execution for smaller slices of time. For that duty you’ll need to code your own function, similar to the delay() function I use.

The delay() function is built upon a C library function called clock().

The clock() function returns a time value in clock ticks, which is based on the processor’s speed. The value returned is of the clock_t variable type. You can use subsequent reads of the clock() function to determine elapsed time.

The following code demonstrates elapsed time as measured by the clock() function:

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

int main()
{
    clock_t tix;

    tix = clock();
    printf("And the clock() function returned %ld.\n",tix);
    tix = clock();
    printf("And the clock() function returned %ld.\n",tix);

    return(0);
}

Here is the sample output I see on my system:

And the clock() function returned 1072.
And the clock() function returned 1107.

Of course, to make the clock() function useful, it helps to know how many clock ticks take place a second. That information is stored in the CLOCKS_PER_SEC constant.

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

int main(void)
{
	clock_t ticks1, ticks2;

	ticks1=clock();
	ticks2=ticks1;
	while((ticks2/CLOCKS_PER_SEC-ticks1/CLOCKS_PER_SEC)<1)
		ticks2=clock();

	printf("It took %ld ticks to wait one second.\n",ticks2-ticks1);
	printf("This value should be the same as CLOCKS_PER_SEC which is %d.\n",CLOCKS_PER_SEC);
	return 0;
}

The above code initializes both clock_t variables to the same value at Line 9. Then a while loop spins until the difference between the values is 1. Here’s the output:

It took 999017 ticks to wait one second.
This value should be the same as CLOCKS_PER_SEC which is 1000000.

Close enough.

Because the CLOCKS_PER_SECOND value could be different on each computer, you need to do some math to figure out how to concoct a delay() function that works on all platforms. Below you see my solution, which uses a millisecond duration as the argument to delay().

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

void delay(int milliseconds);

int main()
{
    int x;

    puts("Loop with 250 millisecond delay:");
    for(x=0;x<10;x++)
    {
        printf("%d\n",x+1);
        delay(250);
    }
    putchar('\n');

    return(0);
}

void delay(int milliseconds)
{
    long pause;
    clock_t now,then;

    pause = milliseconds*(CLOCKS_PER_SEC/1000);
    now = then = clock();
    while( (now-then) < pause )
        now = clock();
}

At Line 26, the delay() function calculates the pause value in milliseconds (one thousandth of a second). The clock_t variables now and then are initialized at Line 27. Then the while loop waits until the proper number of milliseconds have passed.

Feel free to use the delay() function in your code when a short pause is required.

Leave a Reply