lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Scott Vokes <vokes.s <at> gmail.com> writes:

> luasocket's gettime() function may work better as a portable,
> reasonably high-precision timing function. I've used it for
> benchmarking on Windows, BSD, and Linux. "Battery" distributions like
> Lua for Windows tend to already have luasocket.

But (at least on windows) socket.gettime() is by far not high-resolution enough

Check this:

local hrt=require"HiResTimer"
local socket=require"socket"
local function printf(...)
	io.write(string.format(...))
end
for loops=0,100000,1000 do
	printf("%6d loops ",loops)
	local t0=hrt.clock()
	for i=1,loops do local j=math.sin(123) end
	local t1=hrt.clock()
	printf("hrt=%9.3f ms",(t1-t0)*1000)
	local t0=socket.gettime()
	for i=1,loops do local j=math.sin(123) end
	local t1=socket.gettime()
	printf(" socket=%9.3f ms\n",(t1-t0)*1000)
end

Result (truncated)

     0 loops hrt=    0.035 ms socket=    0.000 ms
  1000 loops hrt=    0.327 ms socket=    0.000 ms
  2000 loops hrt=    0.461 ms socket=    0.000 ms
  3000 loops hrt=    0.685 ms socket=    0.000 ms
  4000 loops hrt=    0.905 ms socket=    0.000 ms
  5000 loops hrt=    1.123 ms socket=    0.000 ms
  6000 loops hrt=    1.391 ms socket=    0.000 ms
  7000 loops hrt=    1.565 ms socket=    0.000 ms
  8000 loops hrt=    1.801 ms socket=   15.625 ms
  9000 loops hrt=    2.032 ms socket=    0.000 ms
 10000 loops hrt=    2.244 ms socket=    0.000 ms
:::::
 90000 loops hrt=   22.061 ms socket=   15.625 ms
 91000 loops hrt=   20.248 ms socket=   31.250 ms
 92000 loops hrt=   22.713 ms socket=   15.625 ms
 93000 loops hrt=   22.575 ms socket=   15.625 ms
 94000 loops hrt=   21.200 ms socket=   15.625 ms
 95000 loops hrt=   22.974 ms socket=   15.625 ms
 96000 loops hrt=   21.370 ms socket=   31.250 ms
 97000 loops hrt=   23.359 ms socket=   31.250 ms
 98000 loops hrt=   21.953 ms socket=   15.625 ms
 99000 loops hrt=   22.304 ms socket=   15.625 ms
100000 loops hrt=   22.887 ms socket=   15.625 ms

socket has (again at least on windows) a resolution of 64 ticks/second,
the PerformanceCounter used in HiResTimer about 3500000 ticks/second 
(will vary with the machine your running on)

So I can use 10000 loops to get significant comparable results with hrt,
but need at least 50000000 (!) loops with socket to get near the same 
number of ticks.

Regards Jørgen