Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ffi = require 'ffi'
- local intidx_fnc do
- if ffi.abi('le') then
- function intidx_fnc(n)
- if n<1 or n>4 then
- error("wrong index", 2)
- end
- return n-1
- end
- else
- function intidx_fnc(n)
- if n<1 or n>4 then
- error("wrong index", 2)
- end
- return 4-n
- end
- end
- end
- local intidx_tab do
- intidx_tab = setmetatable({}, {__index = function(self, index)
- local res = intidx_fnc(index)
- rawset(self, index, res)
- return res
- end})
- end
- local function bench(f)
- local before = os.clock()
- f()
- return os.clock() - before
- end
- local function compare(vals)
- for key, value in pairs(vals) do
- print(key, bench(value))
- end
- end
- local num = 1e9
- compare {
- ['function'] = function()
- for i=1,num do
- local a = intidx_fnc(i % 3 + 1)
- end
- end;
- ['table'] = function()
- for i=1,num do
- local a = intidx_tab[i % 3 + 1]
- end
- end;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement