benloran wrote:Robin wrote:
IIRC, unpack is implemented in C, so that would have the advantage of not needing to be interpreted. Lua's fast, but it's not machine code.
That makes sense. I suspected something like that, but the same example on that LuaPerformance site shows the custom unpack as being much faster than Lua's unpack. That's what threw me.
the lua implementation of unpack is faster because... it's not actually the same thing as the builtin unpack
the c unpack has to loop through the provided table to get everything, while the lua implementation just uses a fixed set of direct table accesses; if you only need a certain number of values it will of course be cheaper to do it in lua, but if you need one capable of an arbitrary number of returns then your only choice is unpack. the lua <-> c overhead isn't insignificant either. however, a proper unpack implemented entirely in lua
might be faster than the c version if you're using luajit
benloran wrote:Actually, I take it back. The original intent of test 8 (I believe... I copied this from the LuaPerformance site) was to show that it's better to localize an anonymous function outside of the loop than to just write an anonymous function inside the function call.
a nice thing 5.2 does is interning closures, if it detects that a function has the exact same definition and upvalues as another, it'll reuse the existing one instead of creating a new one. for example:
Code: Select all
-- in 5.1, 100 closures would be created; in 5.2 only 1 is
local x
for _ = 1, 100 do
(function () return x end)()
end
it'll still be slower than defining the function outside of the loop/function/whereever, but not by a whole lot. on the other hand it's a lot slower in 5.1