[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Faster tag methods
- From: Tom Wrensch <twrensch@...>
- Date: Tue, 5 Mar 2002 10:55:52 -0800 (PST)
On Tue, 5 Mar 2002, Luiz Henrique de Figueiredo wrote:
> >I love the idea of tag methods for get/set. What I don't love is all the
> >string processing required in the host environment to use them in a way
> >where I want to match the key name against a set of known key names.
>
> What string processing? The only time a string is processed in the core of Lua
> is when it enters it; a hash value is computed by scanning the string. After
> that, everything is done using this hash value. Hash lookups are supposed to be
> O(1). A lot of effort goes to make sure that these lookups are fast. Having
> fast tables is an implementation goal of Lua. Lua programmers should take
> this for granted and not be afraid of "hidden" costs.
>
> >FieldNameX = 1
> >table[FieldNameX] = x
>
> This is likely to be *slower* than table.FieldNameX = x when FieldNameX
> is a global instead of a string: there will be 2 lookups: one to get the
> value of FieldNameX and another to set the value of table[FieldNameX].
> --lhf
I ran this to find out the timing numbers. Results at the end:
FieldX = 1
function test()
local t = {}
t[FieldX] = 10
t.FieldX = 10
local temp
local c
write("1,000,000 iterations of temp=t.FieldX: ")
c = clock()
for i=1,1000000 do
temp=t.FieldX
end
print(clock()-c)
write("1,000,000 iterations of temp=t[FieldX]:")
c = clock()
for i=1,1000000 do
temp=t[FieldX]
end
print(clock()-c)
write("1,000,000 iterations of temp=t[1]: ")
c = clock()
for i=1,1000000 do
temp=t[1]
end
print(clock()-c)
end
-- RESULTS: (Lua 4.1work4 750MHz PIII, Win2000)
-- 1,000,000 iterations of temp=t.FieldX: 0.2309999999999945
-- 1,000,000 iterations of temp=t[FieldX]:0.4500000000000455
-- 1,000,000 iterations of temp=t[1]: 0.3209999999999127