Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are high resolution calls to get the system time wrong by the time the function returns?

Tags:

c

time

Given a C process that runs at the highest priority that requests the current time, Is the time returned adjusted for the amount of time the code takes to return to the user process space? Is it out of date when you get it? As a measurement taking the execution time of known number of assembly instructions in a loop and asking for the time before and after it could give you an approximation of the error. I know this must be an issue in scientific applications? I don't plan to write software involving any super colliders any time in the near future. I have read a few articles on the subject but they do not indicate that any correction is made to make the time given to you be slightly ahead of the time the system read in. Should I lose sleep over other things?

like image 484
ojblass Avatar asked Dec 13 '22 04:12

ojblass


1 Answers

Yes, they are almost definitely "wrong".

For Windows, the timing functions do not take into account the time it takes to transition back to user mode. Even if this were taken into account, it can't correct if the function returns, and your code hits a page fault/gets swapped out/etc., before capturing the return value.

In general, when timing things you should snap a start and an end time around a large number of iterations to weed out these sort of uncertainties.

like image 151
Michael Avatar answered Jan 12 '23 01:01

Michael