clock

Description

Calculates the time used by the calling process.

#include <time.h>

clock_t clock( void );

Remarks

The clock function tells how much processor time has been used by the calling process. The time in seconds is approximated by dividing the clock return value by the value of the CLOCKS_PER_SEC constant.

In other words, the clock function returns the number of processor timer ticks that have elapsed. A timer tick is approximately equal to 1/CLOCKS_PER_SEC seconds.

In versions of Microsoft C prior to version 6.0, the CLOCKS_PER_SEC constant was called CLK_TCK.

Return Value

The clock function returns the product of the time in seconds and the value of the CLOCKS_PER_SEC constant. If the processor time is not available, the function returns the value –1, cast as clock_t.

In DOS, clock returns the time elapsed since the process started. This may not be equal to the actual processor time used by the process.

Compatibility

Standards:ANSI

16-Bit:DOS, QWIN, WIN

32-Bit:DOS32X

See Also

difftime, time

Example

/* CLOCK.C: This example prompts for how long the program is to run and

* then continuously displays the elapsed time for that period.

*/

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

void sleep( clock_t wait );

void main( void )

{

long i = 600000L;

clock_t start, finish;

double duration;

/* Delay for a specified time. */

printf( "Delay for three seconds\n" );

sleep( (clock_t)3 * CLOCKS_PER_SEC );

printf( "Done!\n" );

/* Measure the duration of an event. */

printf( "Time to do %ld empty loops is ", i );

start = clock();

while( i-- )

;

finish = clock();

duration = (double)(finish - start) / CLOCKS_PER_SEC;

printf( "%2.1f seconds\n", duration );

}

/* Pauses for a specified number of microseconds. */

void sleep( clock_t wait )

{

clock_t goal;

goal = wait + clock();

while( goal > clock() )

;

}

Output

Delay for three seconds

Done!

Time to do 600000 empty loops is 2.0 seconds