Can someone explain why using sleep() or usleep() and clock() together doesn't work. I have also tried using times() instead of clock() and got the same results. I get 0 ticks when I have a sleep() in the loop. I program on Linux, are there any other functions that measure time in millisecond precision. The reason for having a usleep() in the loop is to reduce cpu usage. I only need millisecond, so I put a usleep(10000) in the loop to sleep for 0.01 sec. I know someone will ask why do I need to use clock(), why not just have a float elapsed variable in the loop and just do a elapsed+=0.01; This is a simple loop, I need to use this in more complex loops where there are if statements, syslogs, assignments, calculations... and I cannot be sure how long those operations take.
using namespace std;
clock_t start, end;
start = clock();
end = clock();
cout << "ticks=" << end-start << endl;
cout << "elapsed=" << ((end-start)/(CLOCKS_PER_SEC*1.0)) << " secs" << endl;
BASICALLY... can anyone recommend a reliable way of measure time in millisecond precision, on linux in c/c++. i have been searching for a reliable method for a while now.