optimization question....

hello!

i think i remember reading that accessing table items on a display object is less efficient than accessing a general lua table… if this is the case, is it more efficient to have a separate object with regularly accessed items such as player health than to add them to the player display object itself?

or is it only slower to access parameters from the original display object - eg. player.x

in other words is

[lua]

local player = display.newRect(0,0,10,10)

player.health = 100

[/lua]

less efficient than

[lua]

local player = display.newRect(0,0,10,10)

local playerstats={}

playerstats.health=100

[/lua]

or even

[lua]

local player = {}

player.playergfx = display.newRect(0,0,10,10)

player.health = 100

[/lua]

and which do you normally do?

thanks!

andy

Feel free to put field right on your objects.  The speed is exactly the same.

http://www.youtube.com/watch?v=Qm2dJkqYWaE

The only cautionary note is, “Beware future changes to Corona SDK.”  If you choose a name carelessly, you may choose one that is added in a future version of Corona SDK.

Ex: Right now I do not think there is a mass field for display objects (even when a body is added), so if I added one now and then later Corona added a mass field to objects with bodies, my code might misbehave.

In summary, feel free to add fields to objects, just use good sense in choosing names or a naming convention unlike the current (Corona) convention.

PS - That video was made with an updated version of CBench which you can run on your own (minus this test) here: http://forums.coronalabs.com/topic/36287-cbench-benchmarking-tool-testers-needed/

I will eventually release this tool and the code in some fashion, but right now it is in testing (as per the link above).

At the risk of polluting this thread, here is the code to the two benchmarks:

-- ============================================================= -- Copyright Roaming Gamer, LLC. 2009-2013 -- ============================================================= local benchtype = "ops" -- Measures Raw Operations Per Second local name = "Field Access - Lua Table" local benchOuter = 30 -- Times to run benchmark local benchInner = 100000 -- Number of inner loops to run -- Any prep work you need to do before starting the benchmark  (not measured) local testObj local function prep( group )     testObj = {}     testObj.hitPoints = 0 end -- Any cleanup work you need to do after the benchmark (not measured) local function cleanup( group )     testObj = nil end -- The actual benchmark (measured) local function bench( numOps, group  )     for i = 1, numOps do         testObj.hitPoints = i     end end -- DO NOT EDIT BELOW THIS LINE local public = {} public.benchtype = benchtype public.name = name public.prep = prep public.cleanup = cleanup public.bench = bench public.iterations = benchOuter public.numOps = benchInner return public 

-- ============================================================= -- Copyright Roaming Gamer, LLC. 2009-2013 -- ============================================================= local benchtype = "ops" -- Measures Raw Operations Per Second local name = "Field Access - Display Object" local benchOuter = 30 -- Times to run benchmark local benchInner = 100000 -- Number of inner loops to run -- Any prep work you need to do before starting the benchmark  (not measured) local testObj local function prep( group )     testObj = display.newCircle( centerX, centerY, 20 )     testObj.hitPoints = 0 end -- Any cleanup work you need to do after the benchmark (not measured) local function cleanup( group )     testObj:removeSelf()     testObj = nil end -- The actual benchmark (measured) local function bench( numOps, group  )     for i = 1, numOps do         testObj.hitPoints = i     end end -- DO NOT EDIT BELOW THIS LINE local public = {} public.benchtype = benchtype public.name = name public.prep = prep public.cleanup = cleanup public.bench = bench public.iterations = benchOuter public.numOps = benchInner return public 

cool, thanks , the benchmark code looks really useful!

Thanks Ed!

Feel free to put field right on your objects.  The speed is exactly the same.

http://www.youtube.com/watch?v=Qm2dJkqYWaE

The only cautionary note is, “Beware future changes to Corona SDK.”  If you choose a name carelessly, you may choose one that is added in a future version of Corona SDK.

Ex: Right now I do not think there is a mass field for display objects (even when a body is added), so if I added one now and then later Corona added a mass field to objects with bodies, my code might misbehave.

In summary, feel free to add fields to objects, just use good sense in choosing names or a naming convention unlike the current (Corona) convention.

PS - That video was made with an updated version of CBench which you can run on your own (minus this test) here: http://forums.coronalabs.com/topic/36287-cbench-benchmarking-tool-testers-needed/

I will eventually release this tool and the code in some fashion, but right now it is in testing (as per the link above).

At the risk of polluting this thread, here is the code to the two benchmarks:

-- ============================================================= -- Copyright Roaming Gamer, LLC. 2009-2013 -- ============================================================= local benchtype = "ops" -- Measures Raw Operations Per Second local name = "Field Access - Lua Table" local benchOuter = 30 -- Times to run benchmark local benchInner = 100000 -- Number of inner loops to run -- Any prep work you need to do before starting the benchmark  (not measured) local testObj local function prep( group )     testObj = {}     testObj.hitPoints = 0 end -- Any cleanup work you need to do after the benchmark (not measured) local function cleanup( group )     testObj = nil end -- The actual benchmark (measured) local function bench( numOps, group  )     for i = 1, numOps do         testObj.hitPoints = i     end end -- DO NOT EDIT BELOW THIS LINE local public = {} public.benchtype = benchtype public.name = name public.prep = prep public.cleanup = cleanup public.bench = bench public.iterations = benchOuter public.numOps = benchInner return public 

-- ============================================================= -- Copyright Roaming Gamer, LLC. 2009-2013 -- ============================================================= local benchtype = "ops" -- Measures Raw Operations Per Second local name = "Field Access - Display Object" local benchOuter = 30 -- Times to run benchmark local benchInner = 100000 -- Number of inner loops to run -- Any prep work you need to do before starting the benchmark  (not measured) local testObj local function prep( group )     testObj = display.newCircle( centerX, centerY, 20 )     testObj.hitPoints = 0 end -- Any cleanup work you need to do after the benchmark (not measured) local function cleanup( group )     testObj:removeSelf()     testObj = nil end -- The actual benchmark (measured) local function bench( numOps, group  )     for i = 1, numOps do         testObj.hitPoints = i     end end -- DO NOT EDIT BELOW THIS LINE local public = {} public.benchtype = benchtype public.name = name public.prep = prep public.cleanup = cleanup public.bench = bench public.iterations = benchOuter public.numOps = benchInner return public 

cool, thanks , the benchmark code looks really useful!

Thanks Ed!