I am more or less wondering how time() is implemented in the C standard library and what would happen in the situation described below. Although this time is most-likely negligible, consider a situation where you have a hard-limit on time and no control over the CPU scheduler (assume that it is a “good” scheduler for a general-purpose CPU).
Now, if I use time() to calculate my execution time of a particular section of code and use this time subtracted from some maximum bound to determine some other time-dependent variable, how would this variable be skewed based on context-switches? I know we could use nice and other tools (i.e. custom scheduler, etc.) to be certain we get full CPU usage when we need it, however, I am wondering how this works in general for similar situations as this and what side-effects exist due to the system’s choices.
timeis supposed to measure wall-time. I.e., it gives the current time, regardless of how much or little your process has run.If you want to measure cpu time, you should use
clockinstead (though some vendors such as MS implement it wrong, so it does wall time also).Of course, there are also other tools to retrieve CPU usage, such as
timeson Unix-like systems orGetProcessTimeson Windows. Most people find these more useful despite the reduced portability.