c++ timer not precise enuogh, or I'm doing something wrong

Aug 19, 2010 at 10:17pm
I have:
1
2
3
4
5
6
double start, finish, elapsed;
start = time(NULL);
puzzle.solve();
end = time(NULL);
elapsed = end - start;
cout << "Elapsed time = " << elapsed << endl; 


but its showing "Elapsed time = 0" every time.
Aug 20, 2010 at 2:10am
that's because time() returns the time in seconds. Your computations are taking less than 1 second.

You'll need a higher precision timer, but the standard lib doesn't offer one.

You'll need to look at external libs, or you'll have to do something with the platform's API. If you're on Windows, you can #include <windows.h> and use GetTickCount(), which returns the time in milliseconds.

Get TickCount is the simplest, but even it might not be a high enough resolution.


Disregard me, I'm having an off day.
Last edited on Aug 20, 2010 at 3:56am
Aug 20, 2010 at 3:34am
what about clock() in ctime
Aug 20, 2010 at 3:56am
whoops.

XD

Good call.
Topic archived. No new replies allowed.