Question about LuaJIT and FFI

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Question about LuaJIT and FFI

Post by pgimeno »

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.

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)
Sample run:

Code: Select all

0.20413184165955
1.9447298049927
1.988970041275
Edit: Actually even arrays are slower in interpreted mode. I've updated the benchmark.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests