[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: os.clock conversion to milliseconds under MacOS X
- From: "Gary V. Vaughan" <gary@...>
- Date: Wed, 17 Dec 2014 11:12:06 +0000
> On Dec 17, 2014, at 11:09 AM, Gary V. Vaughan <gary@vaughan.pe> wrote:
>
> 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:
I mean, *micro*second resolution...
> $ 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)