The Art of Software

C++ Function to Time the Execution of Another Function


This is just a simple function to time the execution of an incoming function with arbitrary arguments. I’ve used it successfully with gcc 4.6.1.

#include <ctime>

//! Return the number of clock ticks a function takes to execute.
//! \see
template<typename F, typename ...Args>
clock_t timeFunction(F f, Args&& ...args)
   auto begin = clock();
   auto end = clock();
   return end-begin;