CpuTime()
returns a double
whose value is the user CPU usage in seconds
since the start of the program. To find out how long func()
takes to
execute you can do the following:
int main() { double t0 = CpuTime(); func(); cout << "Time taken (in seconds) is " << CpuTime()-t0 << endl; return 0; }
RealTime()
returns a double
whose value is the number of seconds
elapsed since some fixed point in the past (on Unix/Linux boxes this is
typically 1st January 1970, sometimes called "the epoch").
WARNING: we cannot guarantee the accuracy of these functions; as a rule of thumb you should regard time differences as having an imprecision of around 2% plus upto 0.2 seconds of unknown variation. So a time difference less than 1 second is likely to have quite a large relative error.
It works on GNU/Linux and MacOSX. I hope someone else will deal with the portability issues.
Might not work on Microsoft platforms -- maybe this is really a feature?
Cannot make any guarantees about the accuracy of CpuTime
and RealTime
.
I ignore the return values of getrusage
and gettimeofday
; I'd be
amazed if they could signal errors, but perhaps the code ought to check?