0

I'm creating a simple timer class which returns me e.g. the current time in millis. On linux I'm using gettimeofday. I'm wondering what return type this function should have. i.e. double getMillis() or uint64_t getMillis() etc... I would say uint64_t can hold bigger values and therefore recommended, though while googling I see lots of different implementations.

Any advice on this?

Thanks

pollux
  • 779
  • 2
  • 14
  • 30
  • `uint64_t` can hold bigger values then `double`??? `double` might (or might not) lead to precision issues, but it can definately hold bigger values then `uint64_t`. – Grizzly Sep 23 '12 at 11:28
  • Grizzly if I recall correctly they are both 8bytes, but double is signed + real. – pollux Sep 23 '12 at 11:31
  • 2
    In C++11 we have the header for this. You can do `auto time = std::chrono::system_clock::now().time_since_epoch().count();`. The returned type is `std::chrono::system_clock::rep`. No need for platform specific code. – Joseph Mansfield Sep 23 '12 at 11:38
  • 1
    You might want to take a look at [what every computer scientist should know about floating point](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). `double `has an precision of about 17 digits, but can encode values up to 10^300 or something like this (above that comes infinity of course) – Grizzly Sep 23 '12 at 11:49

2 Answers2

2

My recommended data type to hold absolute time stamps in milliseconds is int64_t, mainly because time_t is signed.

Seg Fault
  • 1,331
  • 8
  • 16
1

I would go with the unsigned integer type since the number of milliseconds is a count. Makes adding and subtracting dependable without float as well. Most implementations I have used has unsigned integer types.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106