Measuring elapsed time in a program?

If only second resolution is required, then os.time() is sufficient. For millisecond accuracy, there is os.clock(). However, there are two gotchas with this little function. The actual precision is likely to be much larger than a millisecond, and the definition of 'process time' in Unix and Windows is fundamentally different. In Unix, os.clock() returns how much processor time has been used, so a script that spends most of its time sleeping will appear to use little time.

socket.gettime() returns fractional seconds, and is cross-platform.

4 Libraries



Back