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

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.
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
what about clock() in ctime
whoops.

XD

Good call.
Topic archived. No new replies allowed.