User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:time.h:clock

Table of Contents

clock

Description

The clock() function returns the processor time since the program started, or -1 if that information is unavailable.
To convert the return value to seconds, divide it by CLOCKS_PER_SEC. (Note: if your compiler is POSIX compliant, then CLOCKS_PER_SEC is always defined as 1000000.)

clock c example

/* 
 * clock example code
 * http://code-reference.com/c/time.h/clock 
 */
#include <stdio.h>
#include <time.h>
 
int main (void)
{
   int sec = 5;
   clock_t end = clock() + sec * CLOCKS_PER_SEC;
 
   while (clock() < end) 
   {
     // do something
   }
 
   return 0;
}

or

/* 
 * clock example code
 * http://code-reference.com/c/time.h/clock 
 */
#include <stdio.h>
#include <time.h>
int main( void )
{
  clock_t start = clock();
  clock_t finish;
  int i;
 
  for (i = 0; i < 9999; ++i) {
    printf("%i\n", i);
  }
 
  finish = clock();
 
  printf("executed \"printf\" %i times\nCocks per Sec: %li\nStart: %li\nFinish: %li\n",i ,  CLOCKS_PER_SEC, start, finish);
  return 0;
}

output

  executed "printf" 9999 times
  Cocks per Sec: 1000000
  Start: 0
  Finish: 20000

see also

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/time.h/clock.txt · Last modified: 2024/02/16 01:04 (external edit)

Impressum Datenschutz