time

© 2005 John Abbott
GNU Free Documentation License, Version 1.2



CoCoALib Documentation Index

User documentation for CpuTime and RealTime

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.

DateTime(long& date, long& time) stores in date and time the current date and time in the format yyyymmdd, hhmmss. Example:

      long date, time_unused;
      DateTime(date, time_unused);
      int YearToday = date/10000;
      int MonthToday = (date/100)%100;
      int DayToday = date%100;

Maintainer documentation for cputime

It works on GNU/Linux and MacOSX. I hope someone else will deal with the portability issues.

Bugs, Shortcomings, and other ideas

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?