Let me get this straight, you took a loop with 1 addition, 1 multiplication,
2 assignments, and 1 comparison and added 2 table accesses to it. After
observing that this slows things down by a factor of ~2 you decided that
table accesses are expensive.
If we count additions, multiplications, assignments, and comparisons all as
"simple" operations, then we have...
1+1+2+1 = 5
A factor of 2 slowdown would then be equivalent to approximately 10 "simple
operations".
These 10 "simple operations" includes the original 5 and 2 table accesses.
Thus a table access is ~ (10-5)/2 "simple operations" or 2.5 "simple
operations".
That sounds pretty good to me.
Of course this isn't free. If you really do need to run a loop half a
million times you probably are going to want to optimize out unnecessary
table accesses (in favor of local variables, etc.).
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Jack
Christensen
Sent: Tuesday, May 04, 2004 5:37 PM
To: Lua list
Subject: Table performance
I'm trying to do as much as possible in Lua before I run into
performance problems and have to drop back to C++. As part of the tests
I was running I came across some surprising (to me anyway) results.
Am I doing something wrong or is it rather expensive to access to a table?
The following two blocks for code are identical except that one loop has
two table accesses in it. The one with tables runs at less then half the
speed.
local x, y, i = 0, 3, 0
while i < 500000 do
x = i * y
i = i + 1
end
local t = { x = 0, y = 3 }
local i = 0
while i < 500000 do
t.x = i * t.y
i = i + 1
end
I also tried binding a simple C++ class with luabind and running the
equivalent loop. It was about 10 times slower than the table loop above.
Jack