lua-users home
lua-l archive

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


I've made interesting discovery, it seems that using math functions from ffi.C, i.e. ffi.C.sin is 2x faster than using built-in math. I have investigated that in context of optimized stateless ffi callbacks idea here: http://lua-users.org/lists/lua-l/2011-12/msg00436.html

Therefore go few question:
• Does it make sense at all to use math.fun in luajit for math intensive scripts?
• Can math.fun calls to be same fast as ffi.C.fun (direct calls)?
• If we load some other library via ffi.load(name, true), does setting global to true implies direct optimized calls via ffi.C.other_lib_fun as well (i.e. optimized lapack calls)? 

-- benchmark -------------------------------------
$ time luajit bench.lua 
3.100828

real	0m4.300s
user	0m4.298s
sys	0m0.002s

$ time luajit bench.lua ffi
3.100828

real	0m2.615s
user	0m2.613s
sys	0m0.002s

-- code -------------------------------------
local S = 100000000
local s = 0

if arg[1] == 'ffi' then
	local ffi = require('ffi')
	ffi.cdef [[
		double sin(double x);
	]]
	for n = 0, S-1 do
		s = s + ffi.C.sin(n/3)
	end
else
	for n = 0, S-1 do
		s = s + math.sin(n/3)
	end
end

print(string.format("%.6f", s))

Cheers,
-- 
Adam Strzelecki