1. 2
  1.  

  2. 1

    Watch out: time_t is not necessarily based on the Unix epoch nor measured in seconds. CppReference.com says:

    Although not defined by the C standard, this is almost always an integral value holding the number of seconds (not counting leap seconds) since 00:00, Jan 1 1970 UTC, corresponding to POSIX time

    I have heard there are platforms where the units are milliseconds.

    1. 1

      Interesting, it’s also notable that:

      ISO C defines time_t as an arithmetic type, but does not specify any particular type, range, resolution, or encoding for it. Also unspecified are the meanings of arithmetic operations applied to time values.

    2. 1

      Elapsed times should be measured with a monotonic clock. time(3) can jump around and even go backwards if the system clock is changed, but clock_gettime(2) with CLOCK_MONOTONIC will (bugs aside) never go backwards and at worst might get skewed a bit by ntp.

      C++ has std::chrono::steady_clock, Rust has std::time::Instant, .NET has Stopwatch, Ruby has Process.clock_gettime(Process::CLOCK_MONOTONIC) (or I have a slightly nicer interface to it in monotime), Python has time.monotonic.

      1. 1

        TIL! Thank you for the feedback :)

      Stories with similar links:

      1. How to Measure Execution Time of a Program authored by serhack 4 months ago | 2 points | no comments