Which one is faster? and Why?
1- Lua Method:
local sprite = Sprite.new()
sprite.child = Sprite.new()
sprite:addChild(self.child)
Doing some calculation with sprite.child
2- Gideros Method:
local sprite = Sprite.new()
local child = Sprite.new()
sprite:addChild(child)
Doing some calculation with sprite:getChildAt(1)
I think using local variable for creating sprites is better but what about this in my code?
local test = self:getParent():getParent():getChildAt(2):getChildAt(3)
Comments
sprite:addChild(self.child)
must be
sprite:addChild(sprite.child)
if local variable is not found then lua is looking at next scope, and next scope, looking up the value each time
Similar with properties, lua first need to lookup table which has this property and then lookup the value in it.
so 2 method is fastest
But about your other code, if you need to getParent only once, then this code is perfect.
if you need to getParent a lot of times in the code, then you better cache parent in the local variable and reuse it
and actually your method for answering questions is fastest.
Likes: ar2rsawseen
http://stackoverflow.com/a/12865406/2036733
Very interesting and informative.
http://www.lua.org/gems/sample.pdf
Keep in mind that our games are only running at 60 fps, so choosing something like a for loop of ipairs will in most cases be insignificant.
My test Result:
self method is faster than getChildAt() method!!!