On 1/25/2017 2:49 PM, Rena wrote:|
I always assumed there were some paintings in a vault that are aging slowly...
According to the clock() manpage , CLOCKS_PER_SEC effectively must be the constant 1000000. I'm not sure why clock_t is permitted to be signed, but it clearly is a signed type on 32-bit x86 linux. Even if unsigned, clock() rolls over after a little less than 72 minutes or 2^32 microseconds.
So perhaps Lua's implementation of os.clock() ought to cast the result of clock() to a platform-specific sized unsigned value since by the time it has been converted to a floating point value it is too late for the Lua code to do much with it in a portable way when it goes negative.
If you did anything like this, it would be to remember the value returned from the first call to clock() and subtract that from all subsequent calls, and possibly make that first call to clock() as part of initializing the os module so that 0 is close to the actual start of the program. The Lua manual  does say "Returns an approximation of the amount in seconds of CPU time used by the program" so taming the fact that POSIX permits clock() to return anything at all at the start of the process might be arguably the correct thing to do. Or relax the documentation to merely claim that os.clock() will be monotonic until it wraps, which might be in 72 minutes, or until the heat death of the universe, depending.
Either way, it does seem like a bug that os.clock() could return a negative number, and fixing that would not be difficult. Preventing the wrap to zero on 32-bit systems would be substantially more difficult, technically possible, and likely completely not worth the effort. It could also be done in pure Lua, if you can make the assumption that os.clock() will be called often enough.
-- Ross Berteig Ross@CheshireEng.com Cheshire Engineering Corp. http://www.CheshireEng.com/ +1 626 303 1602