-
Notifications
You must be signed in to change notification settings - Fork 2
/
hires.lua
46 lines (38 loc) · 966 Bytes
/
hires.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
local ffi = require("ffi")
ffi.cdef[[
struct timeval {
uint64_t tv_sec;
uint64_t tv_usec;
};
struct timespec {
uint64_t tv_sec;
long tv_nsec;
};
uint64_t time(uint64_t *t);
int gettimeofday(struct timeval *tv, struct timezone *tz);
int clock_gettime(int clk_id, struct timespec *tp);
]]
timeval = ffi.typeof("struct timeval");
timespec = ffi.typeof("struct timespec");
local CLOCK_REALTIME = 0
local CLOCK_MONOTONIC = 1
local CLOCK_PROCESS_CPUTIME_ID = 2
local CLOCK_THREAD_CPUTIME_ID = 3
local CLOCK_MONOTONIC_RAW = 4
local CLOCK_REALTIME_COARSE = 5
local CLOCK_MONOTONIC_COARSE = 6
local C = ffi.C
local function clock()
local ts = timespec()
local x = C.clock_gettime(CLOCK_THREAD_CPUTIME_ID,ts)
return tonumber(ts.tv_sec) + tonumber(ts.tv_nsec)/1e9;
end
local function hitime()
local tv = timeval();
C.gettimeofday(tv,nil);
return tonumber(tv.tv_sec) + tonumber(tv.tv_usec)/1e6;
end
return {
time = hitime;
clock = clock;
}