lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Dec 17, 2014, at 9:50 AM, Marco Bambini <marco@sqlabs.net> wrote:
> what is the correct way to convert a difference between two os.clock values in milliseconds on MacOS X?

os.clock() only has 1-second resolution, but if the nearest 1000ms is enough for you then:

    $ lua
    Lua 5.3.0  Copyright (C) 1994-2014 Lua.org, PUC-Rio
    > began = os.clock ()
    > local elapsed = os.difftime (os.clock (), began)
    > io.write (string.format ("%dms elapsed\n", 1000 * elapsed))
    1000ms elapsed

otherwise, you might find the luaposix[1] time functions, with nanoseconds resolution, more useful:

    $ lua
    Lua 5.3.0  Copyright (C) 1994-2014 Lua.org, PUC-Rio
    > posix = require 'posix'
    > timersub, gettimeofday = posix.timersub, posix.gettimeofday
    > began = gettimeofday ()
    > elapsed = timersub (gettimeofday (), began)
    > io.write (string.format ("%.0fms elapsed\n", elapsed.sec * 1000 + elapsed.usec / 1000))
    1758ms elapsed

HTH,
-- 
Gary V. Vaughan (gary AT vaughan DOT pe)