Last active
September 16, 2018 09:25
-
-
Save CapsAdmin/a04502e2e2a2054e345dd6f6599e0786 to your computer and use it in GitHub Desktop.
struct_benchmark_test.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ffi = require("ffi") | |
local tests = { | |
aligned_float = [[ | |
struct __attribute__(( aligned(16) )) NAME { | |
__attribute__(( packed )) | |
float x, y, z; | |
}; | |
]], | |
aligned_double = [[ | |
struct __attribute__(( aligned(16) )) NAME { | |
__attribute__(( packed )) | |
float x, y, z; | |
}; | |
]], | |
float = [[ | |
struct NAME { | |
float x, y, z; | |
}; | |
]], | |
double = [[ | |
struct NAME { | |
double x, y, z; | |
}; | |
]] , | |
} | |
for name, declaration in pairs(tests) do | |
name = "gtest_" .. name .. "_ct" | |
declaration = declaration:gsub("NAME", name) | |
ffi.cdef(declaration) | |
local ctor | |
local meta = { | |
__add = function(v1, v2) return ctor(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z) end, | |
__sub = function(v1, v2) return ctor(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z) end, | |
__mul = function(v1, v2) return ctor(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z) end, | |
__div = function(v1, v2) return ctor(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z) end, | |
} | |
ctor = ffi.metatype("struct " .. name, meta) | |
local vec3 = ctor | |
do | |
io.write(name, " multiplication: ") | |
local t = os.clock() | |
local v = vec3(math.random(), math.random(), math.random()) | |
pairs({}) -- trace barrier | |
for i = 1, 100000000 do | |
v = v * v | |
end | |
pairs({}) -- trace barrier | |
t = os.clock() - t | |
io.write(t, " seconds\n") | |
end | |
do | |
io.write(name, " addition: ") | |
local t = os.clock() | |
local v = vec3(math.random(), math.random(), math.random()) | |
pairs({}) -- trace barrier | |
for i = 1, 100000000 do | |
v = v + v | |
end | |
pairs({}) -- trace barrier | |
t = os.clock() - t | |
io.write(t, " seconds\n") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment