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:

Therefore go few question:
• Does it make sense at all to use in luajit for math intensive scripts?
• Can calls to be same fast as (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 

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

$ time luajit bench.lua ffi

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)
	for n = 0, S-1 do
		s = s + math.sin(n/3)

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

Adam Strzelecki