cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : ctime (time.h) : clock
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
ctime (time.h)
functions:
· asctime
· clock
· ctime
· difftime
· gmtime
· localtime
· mktime
· strftime
· time
macros:
· CLOCKS_PER_SEC
· NULL
types:
· clock_t
· size_t
· time_t
· struct tm

-

clock function
clock_t clock ( void );
<ctime>

Clock program

Returns the number of clock ticks elapsed since the program was launched.

The macro constant expression CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).

The initial moment of reference used by clock as the beginning of the program execution may vary between platforms. To calculate the actual processing times of a program, the value returned by clock should be compared to a value returned by an initial call to clock.

Parameters

(none)

Return Value

The number of clock ticks elapsed since the program start.

On failure, the function returns a value of -1.

clock_t is a type defined in <ctime> to some type capable of representing clock tick counts and support arithmetical operations (generally a long integer).

Example

/* clock example: countdown */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main ()
{
  int n;
  printf ("Starting countdown...\n");
  for (n=10; n>0; n--)
  {
    printf ("%d\n",n);
    wait (1);
  }
  printf ("FIRE!!!\n");
  return 0;
}

Output:


Starting countdown...
10
9
8
7
6
5
4
3
2
1
FIRE!!!

See also

time Get current time (function)
difftime Return difference between two times (function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us