Chapter Contents

Previous

Next
clock

clock



Measure Program Processor Time

Portability: ISO/ANSI C conforming


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <time.h>

clock_t clock(void);


DESCRIPTION

clock returns the amount of processor time used by the program relative to some base point. The amount of processor time used between two calls is indicated by the difference between the values returned by the two calls to clock in the same program.

The value returned is of type clock_t . The value returned is in fractions of a second, where a value of CLOCKS_PER_SEC represents one second of processor time. ( clock_t and CLOCKS_PER_SEC are defined in <time.h> .) In this implementation, clock_t is defined as a double and CLOCKS_PER_SEC is 1.0.


RETURN VALUE

clock returns the number of seconds since the base time. If an accurate value cannot be returned, (clock_t)-1 is returned.


CAUTION

The value returned by clock is of relatively low accuracy and may depend on the extent of other system activity. Values returned by clock are likely to be inconsistent from one execution of a program to another.


PORTABILITY

For portability's sake, you should always use CLOCKS_PER_SEC as a scale factor when using the value returned by clock . Also, you should declare variables that contain clock values as clock_t because many implementations define this type as long int or unsigned long int .


IMPLEMENTATION

Under OS/390, the base point for clock is the first call; that is, the first call of clock in an OS/390 program always returns 0.0.

If the program calls the system function, processor time subsequently used by invoked programs is not included in the value returned by clock .

Under CMS, the base point for clock is the total processor time ( TOTCPU ) as returned by DIAGNOSE X'0C'. If the accumulated time is reset by the system operator after a call to clock , clock returns -1.0 thereafter because the amount of the processor time used can no longer be determined.


EXAMPLE

This example determines the processor time required to compute 1000 logarithms.

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

main()
{
   clock_t start, end;
   double index;

      /* time used before start of computation */
   start = clock();
   for(index = 1.0; index <= 1000.0; ++index)
      (void)log(index);
   end = clock();
   printf("Processor time used = %g seconds.\n",
          (end - start) / CLOCKS_PER_SEC);
}


RELATED FUNCTIONS

alarm , time


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.