Last active
January 11, 2018 13:15
-
-
Save calio/49653917ba24e4bec067 to your computer and use it in GitHub Desktop.
clock_gettime() via LuaJIT + FFI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ffi = require("ffi") | |
ffi.cdef[[ | |
typedef long time_t; | |
typedef int clockid_t; | |
typedef struct timespec { | |
time_t tv_sec; /* seconds */ | |
long tv_nsec; /* nanoseconds */ | |
} nanotime; | |
int clock_gettime(clockid_t clk_id, struct timespec *tp); | |
]] | |
function clock_gettime() | |
local pnano = assert(ffi.new("nanotime[?]", 1)) | |
-- CLOCK_REALTIME -> 0 | |
ffi.C.clock_gettime(0, pnano) | |
return pnano[0] | |
end | |
local nano = clock_gettime() | |
print(nano.tv_sec) | |
print(nano.tv_nsec) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment