Implementing general timers

can some one give me some advice on how to implement a timer..
#include <time.h>

-Albatross
thanks........

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sys/time.h>
using namespace 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;
}
Last edited on
thanks man i'm gonna give that a try....

have you tried using #include <ctime>??
Yeah. ctime doesn't work. I think it's just something funny about the setup of the library files.
Topic archived. No new replies allowed.