-
-
Save perusio/efd578483f476b75224b 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