Re: Question about LuaJIT and FFI
Posted: Fri Dec 25, 2020 1:12 pm
Besides what slime says, FFI arrays and pointers should be faster than Lua tables or sequences, but in my experience, structs with named fields are significantly slower than tables in interpreted mode.
Sample run:
Edit: Actually even arrays are slower in interpreted mode. I've updated the benchmark.
Code: Select all
local ffi = require 'ffi'
local gettime = require 'socket'.gettime
local start, finish
local limit = 10000000
jit.off()
local t1 = {a = 0, b = 0}
start = gettime()
for i = 1, limit do
t1.a = t1.a + 5
t1.b = t1.b + 2
end
finish = gettime()
print(finish - start)
ffi.cdef[[
typedef struct {
double a;
double b;
} t_struct;
]]
local t2 = ffi.new('t_struct')
t2.a = 0
t2.b = 0
start = gettime()
for i = 1, limit do
t2.a = t2.a + 5
t2.b = t2.b + 2
end
finish = gettime()
print(finish - start)
local t3 = ffi.new('double[?]', 2)
t3[0] = 0
t3[1] = 0
start = gettime()
for i = 1, limit do
t3[0] = t3[0] + 5
t3[1] = t3[1] + 2
end
finish = gettime()
print(finish - start)
Code: Select all
0.20413184165955
1.9447298049927
1.988970041275