[SOLVED] Functions Performance

Hello Community, I have a question, what’s better? Coding with functions inside another functions or create functions separately?

Examples:

Inside another function

--Spawn Ball  
function spawnBalls:tap ()  
 ball = display.newImage ( "Ball.png" )  
 ball:setReferencePoint ( display.CenterReferencePoint )  
 ball.x = mRandom ( 40, wScreen - 40 )  
 ball.y = mRandom ( 40, hScreen - 40 )  
 physics.addBody ( ball, ballObject )  
 ball.myName = "ball"  
 ball:addEventListener ( "collision", ball )  
 --Remove Ball Collision  
 function ball:collision (e)  
 if ( e.other.myName == "ball" ) then  
 e.target:removeSelf ()  
 e.target = nil  
 print ( "Removed Ball" )  
 end  
 end  
 end  
--Listener  
Runtime:addEventListener ( "tap", spawnBalls )  

Separate

[code]
–Spawn Circles
function spawnCircles ()
local circle = display.newImage ( “GreenBall.png” )
circle:setReferencePoint ( display.CenterReferencePoint )
circle.x = mRandom ( 50, wScreen - 50 )
circle.y = mRandom ( 50, hScreen - 100 )
screenCircles = screenCircles + 1
circle:addEventListener ( “tap”, removeCircles )
–Call Start Game Function
startGame ()
end

–Remove Circles
function removeCircles (e)
if ( gameReady == true ) then
media.playEventSound ( tapSound )
e.target:removeSelf ()
screenCircles = screenCircles - 1
–Getting Status Game
statusGame ()
return true
end
end [import]uid: 81091 topic_id: 18033 reply_id: 318033[/import]

We used our Corona® Profiler program to find out :o). We modified your app a bit so that it will do the exact same thing for control.

declared outside

local function collision(self, e)  
 if ( e.other.myName == "ball" ) then  
 e.target:removeSelf ()  
 e.target = nil  
 print ( "Removed Ball" )  
 end  
 end  
function spawnBalls:enterFrame ()  
 ball = display.newImage ( "Icon.png" )  
 ball:setReferencePoint ( display.CenterReferencePoint )  
 ball.x = mRandom ( 40, \_W - 40 )  
 ball.y = mRandom ( 40, \_H - 40 )  
 physics.addBody ( ball, ballObject )  
 ball.myName = "ball"  
 ball:addEventListener ( "collision", ball )  
 --Remove Ball Collision  
 ball.collision = collision  
 end  
--Listener  
Runtime:addEventListener ( "enterFrame", spawnBalls )  

and for inside the code

  
function spawnBalls:enterFrame ()  
 ball = display.newImage ( "Icon.png" )  
 ball:setReferencePoint ( display.CenterReferencePoint )  
 ball.x = mRandom ( 40, \_W - 40 )  
 ball.y = mRandom ( 40, \_H - 40 )  
 physics.addBody ( ball, ballObject )  
 ball.myName = "ball"  
 ball:addEventListener ( "collision", ball )  
 --Remove Ball Collision  
 function ball:collision(e)  
 if ( e.other.myName == "ball" ) then  
 e.target:removeSelf ()  
 e.target = nil  
 print ( "Removed Ball" )  
 end  
 end  
 end  
--Listener  
Runtime:addEventListener ( "enterFrame", spawnBalls )  

It is faster to declare collision listeners as a local function outside. However, it takes a bit longer to actually run the collision listener when declared outside. The outside declaration is about 100ms faster with a 30 second profile time (not that significant boost but it can add up)

If you like this report please be sure to check us out at
http://www.mygamedevelopers.com/Corona--Profiler.html
-M.Y. Developers [import]uid: 55057 topic_id: 18033 reply_id: 69002[/import]

Wow! You have excellent tools guys… Thank you so much… Was useful… [import]uid: 81091 topic_id: 18033 reply_id: 69011[/import]

You are welcome and thank you for your support :slight_smile:
-M.Y.Developers [import]uid: 55057 topic_id: 18033 reply_id: 70183[/import]