lua-users home
lua-l archive

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


Obviously it was a contrived example. I'm just trying to get a feel for the relative cost of different activities, and how much can realisticly be done in lua versus native code. What I'm looking at the possibility of is a game creation system that runs lua entirely. This means some performance critical drawing loops that typically would be in C++ could end up in Lua. For example, this snippet:

   for x = 0, this.map.size.x - 1 do
       for y = 0, this.map.size.y - 1 do
this.map.tileList:draw( x * this.map.cellSize.x, y * this.map.cellSize.y, this.map(x,y) ) end
   end

Obviously a lot of table accesses and some metatable hits and most could be eliminated. But for me, one of the perks of using Lua is how much fewer lines of code have to be written to produce the same result as C++. I've tested the above snippet in a more real world scenerio and optimized out all possible table accesses. The performance difference was 0-15%. Not too big a deal.

I'm also using inheritance with metatables so I want to determine how much of a hit I'm taking if many accesses trigger 3-4 __index hits.

I really like being able to write 10-20 lines of Lua instead of 50-100 lines of C++. I just want to make sure I don't run out of CPU cycles too quickly.

Jack

Virgil Smith wrote:

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