COHERENT manpages

This page displays the COHERENT manpage for clock() [Get processor time].

List of available manpages
Index


clock() -- Time Function (libc)

Get processor time
#include <time.h>
clock_t clock();

The function clock() calculates and  returns the amount of processor time a
program  has taken  to execute  to  the current  point.  Execution  time is
calculated from the time the program was invoked.  This, in turn, is set as
a point from the beginning of an era that is defined by the implementation.
Under  COHERENT,  time is  recorded  as the  number  of milliseconds  since
January 1, 1970, 0h00m00s GMT.

The value  clock() returns is of  type clock_t, which is  defined in header
file.  time.h.  If clock() cannot  determine execution time,  it returns -1
cast to clock_t.

To calculate  the execution time  in seconds, divide the  value returned by
clock() by the value of the macro CLK_TCK, which is also defined in time.h.

Example

This example measures the number of  times a for loop can run in one second
on your system.  This is approximate  because CLK_TCK can be a real number,
and because the program probably will not start at an exact tick boundary.

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

main()
{
    clock_t finish;
    long i;

    /* finish = about 1 second from now */
    finish = clock() + CLK_TCK;
    for(i = 0; finish > clock(); i++)
        ;

    printf("The for() loop ran %ld times in one second.\n", i);
    return(EXIT_SUCCESS);
}

See Also

difftime(),
libc,
mktime(),
time.h
ANSI Standard, §.12.2.1