Here's what I used for starting and stopping a timer. Of course you could put it in a timer function. You have to be careful with the accuracy, in that it's not as accurate as it shows:
(I'm not quite sure why I had to include sys/time.h and not just time.h... that's just where it was)
#include <iostream>
#include <sys/time.h>
usingnamespace std;
int main()
{
timeval start, end;
int i;
double t1, t2, differ;
// Get time accurate to ~microseconds (much less accurate in reality)
gettimeofday(&start, NULL);
// Convert time format to seconds.microseconds double
t1 = start.tv_sec + start.tv_usec/1000000.0;
// Do stuff you want timed here
// ...
// ...
// Get ending time
gettimeofday(&end, NULL);
t2 = end.tv_sec + end.tv_usec/1000000.0;
// See how much time passed between beginning and end
differ = t2 - t1;
cout << "\n"<< differ << " seconds have passed\n";
return 0;
}