lua-users home
lua-l archive

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


On Mon, Dec 17, 2012 at 3:02 PM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> But we ought to measure these things first before speculating - I'll
> check with an actual computer tomorrow.

In fact, I should check with a computer before even replying, because
the results were interesting.

Here is a varargs sum function using select (note the local alias)

local select = select

function sum_select (...)
    local sum = 0
    local n = select('#',...)
    for i = 1,n do
        sum = sum + select(i,...)
    end
    return sum
end

which we compare with sum_arr where the loop looks like this:

    local args = {...}
    for i = 1,n do
        sum = sum + args[i]
    end

And to my surprise, the first one is slightly faster.  sum_arr gets a
little bit faster when GC is disabled, but not much.

So a solution using a function is faster than using a table, but the
difference isn't much. The GC has to work much harder (a million calls
at about 156 bytes per call) but it is not slowing us down
significantly.

Array indexing is still faster than select(i,....), but that {....} costs us.

Moral of story?  The difference isn't significant, unless you are
really hammering that function and your system can't recycle memory
fast enough.

https://gist.github.com/4326807

steve d.