User Tools

Site Tools


c:timer

C - Timer

Shows how long a piece of code takes to execute.

clock() returns the number of ticks since the start of the process, the type clock_t is usually long int-I, to get the number of seconds you have this value divided by CLOCKS_PER_SEC. Since the second is for us very carefully, and we do not want to use the float Facilities, translated immediately to milliseconds.

#include <stdio.h>
#include <time.h>
 
#define CLOCKS_PER_MSEC (CLOCKS_PER_SEC / 1000)
 
int main() {
  clock_t clock_start, clock_stop;
  clock_start = clock();
 
  /* It does not run for some time. */
  int i;
  for ( i = 0 ; i < 10000000 ; i++ );
 
  clock_stop = clock();
 
  long int seconds;
  seconds = (clock_stop - clock_start) / CLOCKS_PER_MSEC;
 
  printf("Idle: %ld milliseconds.\n",seconds);
  return 0;
}
c/timer.txt · Last modified: 2020/07/15 10:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki