I want to measure the execution time of some part of my program. Therefore I've written something like
This seems to work fine under Windows. However, running the same program under UNIX (sunOS) produced results that didn't appear to be correct to me.Code:... clock_t startTime = clock(); // do operations clock_t endTime = clock(); std::cout << "elapsed time: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << std::endl; ...
Therefore, I decided double check the time measurement using the time() function:
This program confirmed my doubts... I got results like 204.04 for the measurement using clock() and 426 for the measurement using time(). CLOCKS_PER_SEC has a value of 1000000.Code:... time_t startTime = time(NULL); clock_t startClk = clock(); // do operations clock_t endClk = clock(); time_t endTime = time(NULL); std::cout << "elapsed time: " << (double)(endClk - startClk) / CLOCKS_PER_SEC << std::endl; std::cout << "elapsed time (using time()): " << endTime - startTime << std::endl; ...
The time measurement works fine under windows, i.e. the measurements using clock() and time() yield the same results that appear reasonable to me.
Can somebody explain me what is going wrong under UNIX? And how to solve the problem? The reason I don't want to use time() is, that I have some parts where a resolution of 1 second is not sufficient.