[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Long chains of objects (aka tables)
- From: Petite Abeille <petite.abeille@...>
- Date: Sat, 17 May 2014 20:27:26 +0200
On May 17, 2014, at 7:37 PM, Coda Highland <chighland@gmail.com> wrote:
> The nil metatable hack is substantially faster than actually implementing the necessary
> logical checks (e.g. a and a.b and a.b.c).
Plus it’s not really that bad… for example:
—8<—
local function get( aTarget, ... )
for anIndex = 1, select( '#', ... ) do
local aKey = select( anIndex, ... )
aTarget = aTarget[ aKey ]
if aTarget == nil then
break
end
end
return aTarget
end
local aTable = { { 1, { 1, 2, { 1, 2, 3, { 1, 2, 3, 4, { 1, 2, 3, 4, 5, { 1, 2, 3, 4, 5, 6, { 1, 2, 3, 4, 5, 6, 7, { 1, 2, 3, 4, 5, 6, 7, 8, 9 } } } } } } } } }
for _ = 1, 10000 do
get( aTable, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
end
—>8—
% time lua TestTable.lua
real 0m0.034s
user 0m0.028s
sys 0m0.003s
—8<—
debug.setmetatable( nil, { __index = function() return nil end } )
local aTable = { { 1, { 1, 2, { 1, 2, 3, { 1, 2, 3, 4, { 1, 2, 3, 4, 5, { 1, 2, 3, 4, 5, 6, { 1, 2, 3, 4, 5, 6, 7, { 1, 2, 3, 4, 5, 6, 7, 8, 9 } } } } } } } } }
for _ = 1, 10000 do
_ = aTable[ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ][ 7 ][ 8 ][ 9 ]
end
—>8—
$ time lua TestTable.lua
real 0m0.013s
user 0m0.007s
sys 0m0.003s
So about 3 time slower. One will not even notice in the grand scheme of things .