Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Time It - C

DZone's Guide to

Time It - C

·
Free Resource
This function runs another function, given as a parameter, a certain number of times and returns the number of cycles needed to complete.

Useful for timing short snippets of code.


#include 
  
   

typedef void(*FN)();

double timeit(FN f, int n) {
  clock_t start = clock();

  int i = 0;
  for (; i < n; ++i)
    f();

  return clock() - start;
}

  
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}