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