Skip to content

Commit

Permalink
unimportant
Browse files Browse the repository at this point in the history
  • Loading branch information
capr committed Mar 19, 2018
1 parent 603de2a commit 0041fbf
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions time.lua
Expand Up @@ -21,15 +21,15 @@ if ffi.os == 'Windows' then

function M.time()
C.time_GetSystemTimeAsFileTime(t)
return tonumber(t[0] - DELTA_EPOCH_IN_100NS) / 10^7
return tonumber(t[0] - DELTA_EPOCH_IN_100NS) * 1e-7
end

assert(C.time_QueryPerformanceFrequency(t) ~= 0)
local qpf = tonumber(t[0])
local inv_qpf = 1 / tonumber(t[0]) --precission loss in e-10

function M.clock()
assert(C.time_QueryPerformanceCounter(t) ~= 0)
return tonumber(t[0]) / qpf
return tonumber(t[0]) * inv_qpf
end

function M.sleep(s)
Expand All @@ -54,7 +54,7 @@ elseif ffi.os == 'Linux' or ffi.os == 'OSX' then
function M.sleep(s)
local int, frac = math.modf(s)
t.s = int
t.ns = frac * 10^9
t.ns = frac * 1e9
local ret = C.time_nanosleep(t, t)
while ret == -1 and ffi.errno() == EINTR do --interrupted
ret = C.time_nanosleep(t, t)
Expand All @@ -74,7 +74,7 @@ elseif ffi.os == 'Linux' or ffi.os == 'OSX' then
local clock_gettime = ffi.load'rt'.time_clock_gettime

local function tos(t)
return tonumber(t.s) + tonumber(t.ns) / 10^9
return tonumber(t.s) + tonumber(t.ns) / 1e9
end

function M.time()
Expand Down Expand Up @@ -109,14 +109,14 @@ elseif ffi.os == 'Linux' or ffi.os == 'OSX' then

function M.time()
assert(C.time_gettimeofday(t, nil) == 0)
return tonumber(t.s) + tonumber(t.us) / 10^6
return tonumber(t.s) + tonumber(t.us) * 1e-6
end

--NOTE: this appears to be pointless on Intel Macs. The timebase fraction
--is always 1/1 and mach_absolute_time() does dynamic scaling internally.
local timebase = ffi.new'time_mach_timebase_info_data_t'
assert(C.time_mach_timebase_info(timebase) == 0)
local scale = tonumber(timebase.numer) / tonumber(timebase.denom) / 10^9
local scale = tonumber(timebase.numer) / tonumber(timebase.denom) / 1e9
function M.clock()
return tonumber(C.time_mach_absolute_time()) * scale
end
Expand All @@ -125,6 +125,7 @@ elseif ffi.os == 'Linux' or ffi.os == 'OSX' then

end --Linux or OSX


if not ... then
io.stdout:setvbuf'no'
local time = M
Expand All @@ -141,12 +142,13 @@ if not ... then
time.sleep(ss)
end
local t1 = time.clock()
print(string.format(' missed by: %0.2fms', (t1 - t0 - s) * 1000))
print(string.format(' missed by: %0.2fms', (t1 - t0 - s) / times * 1000))
end

test_sleep(0.001, 0.001)
test_sleep(0.2, 0.02)
test_sleep(2, 0.2)
end


return M

0 comments on commit 0041fbf

Please sign in to comment.