lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 16-06-23 02:53 AM, Viacheslav Usov wrote:
> So for n > 9, there is no reason to prefer select over {...}.

For my test case iterating table after table.pack(...) is faster
than direct select()-ing when n >= 7.

--[[
  Tests relative performance between direct select() and
  table.pack() and iterating table for variadic functions.
]]

local op_1 =
  function(...)
    local num_args = select('#', ...)
    for i = 1, num_args do
      local term = select(i, ...)
    end
  end

local op_2 =
  function(...)
    local args = table.pack(...)
    local num_args = args.n
    for i = 1, num_args do
      local term = args[i]
    end
  end

local list_size = 10
local list = {}
for i = 1, list_size do
  list[i] = i
end

local test =
  function(op)
    local result = 0
    local start_time = os.clock()
    local finish_time = start_time + 1
    while (os.clock() <= finish_time) do
      op(table.unpack(list))
      result = result + 1
    end
    return result
  end

print(('Number of arguments: %d'):format(list_size))
print(('Handle with select()  : %10d / sec'):format(test(op_1)))
print(('Handle with table.pack: %10d / sec'):format(test(op_2)))