[Tips] Optimization 101

@toby2, ah, of course. I didn’t see (or rather my brain didn’t quite register) the guy+colon there in Danny’s example. Yes, now you’ve made it super clear to me. Thank you so much!

Naomi [import]uid: 67217 topic_id: 18550 reply_id: 105943[/import]

Updated the destroy part of the spawn example as you can simply pass self to display.remove and nil out self as self is passed in the declaration. [import]uid: 84637 topic_id: 18550 reply_id: 106049[/import]

Number 2 is not totally correct from my tests, if you cache obstacle.x in a local variable within a local function then update the cahced variable and update the objetc.x later it seems to be quicker than translate from my tests with parralax scrolling. [import]uid: 118379 topic_id: 18550 reply_id: 106060[/import]

Opps just read the previous posts, Don already mentioned it in a sense. [import]uid: 118379 topic_id: 18550 reply_id: 106062[/import]

I thought that transition.to would give me the same kind of boost
so I turned this:

theActual.xScale = _G.currentScale;
theActual.yScale = _G.currentScale;
theActual.x = x*(40 * _G.currentScale)+(20 * _G.currentScale)
theActual.y = y* (40 * _G.currentScale)+(20 * _G.currentScale)
into this:
transition.to(theActual,{time=0,xScale = _G.currentScale,yScale = _G.currentScale, x = x*(40 * _G.currentScale)+(20 * _G.currentScale), y= y* (40 * _G.currentScale)+(20 * _G.currentScale) })
And it slowed down
Is that to be expected?

(I am about to reduce the calcs independently of this… this issue is that I was expecting transition.to to produce an improvement in the same way as the translate() tip)

Any comments?
[import]uid: 108660 topic_id: 18550 reply_id: 110711[/import]

wow…damn good!!! [import]uid: 25057 topic_id: 18550 reply_id: 119184[/import]

Great post! thanks @danny, a few of these optimizations are new to me and we’re going to take them into consideratio in our next game :slight_smile:
[import]uid: 80469 topic_id: 18550 reply_id: 119196[/import]

Awesome post.  V helpful.  :slight_smile:

Does anyone know if there is any penalty for registering multiple event listeners.  For example if one was to add three “enterFrame” listeners that perform separate distinct functions is that more costly than registering a single listener that performs all three tasks.

The former would be preferable from a code clarity P.O.V.

 would the first tip be applied when i will just use the math.rand function for just once for the whole game?

this is realy useful info Danny one q I would like to get an answer on -and its been talked about some times now, is what is most efficient: local myFunc = function( ) or local function myFunc( ) and why?

yow DANNY!! thnx so much for this speed gems!! my eyes were glowing as i was reading the tips! ^_______^  i laughed at my “bad way” programming.daym! am such a transgressor!  looking toward for MOOORE!! hope you keep us posted :slight_smile:

where is the updated post

Using an enum to compare strings is only for when you have a specific set of strings you are working with, correct?

Great Tips :slight_smile: Helps me alot!! Thank yuo very much

Great list learned a lot. Particularly the ‘params’ tip and creating functions within the spawn function.

about #6 :

There is an issue with that example (at least for me). It doesnt work - particulary the ‘for’ loop at the end which limit is #myButtons.

I found that as I got to the point in my project when i have a table of tables:

local TierData = { ['tier1'] = { oneBlahData = 1, otherBlahData = 'someBlahString', }, ['tier2'] = { oneBlahData = 2, otherBlahData = 'someBlahOtherString', }, } 

and i’ve tried to access it by going TierData[i], etc. But first i’d like to do an error check:

local function calculateSegmentProbability(tier) local t = --do some stuff to convert 'tier1' to 1 if (t\>#TierData) then print("Warning in 'calculateSegmentProbability' - dont have next tier data, using last data found instead...") t = #TierData end --do stuff end

and it’s failing, because #TierData is returning ‘0’ - zero.

One way of solving it is to loose the [‘tierX’] handlers, but they’re really informative. From http://lua-users.org/wiki/TablesTutorial at the bottom i get that this form of table is called “unordered set”, but there isn’t any information about how to get the length of that besides looping through all elements - which i assume using is done by using ‘pairs’.

Any one have any ideas of how to solve it?

Thanks for sharing such a great optimization technique. It helped me a lot because I had soem problems in optimization.

I am loving #6 it is really shortening the amount of repeating myself I am doing with button functions.

#1, Is not necessary when using LuaJIT.

Does corona use LuaJIT?

hi, i try the tip with the buttons but with the lib widget.newbutton

the first example without improvements

--REQUIRED------------------------------------------------------------------------------------------------------------------------------------- local widget = require( "widget" ) --TEXT INFORMATION------------------------------------------------------------------------------------------------------------------------------------- local textInfo1 = display.newText("", 240,105,native.systemFont,100) textInfo1:setFillColor(1,0,0) isButtonClick = 0 local function buttonTouchLeft( event )     if ( event.phase == "began" and isButtonClick == 0 ) then     textInfo1.text = "(".."touchleft"..")"     end end   local function buttonTouchRight( event )     if ( event.phase == "began" and isButtonClick == 0 ) then     textInfo1.text = "(".."touchright"..")"    end return textInfo1 end local button1 = widget.newButton {     width = 240,     height = 120,     defaultFile = "buttoninactif.png",     overFile = "buttonpressed.png",     label = "button",     onEvent = buttonTouchLeft } button1.x = 53 button1.y = 225 button1.xScale = 0.5 button1:setLabel(  ) local button2 = widget.newButton {     width = 240,     height = 120,     defaultFile = "buttoninactif.png",     overFile = "buttonpressed.png",     label = "button",     onEvent = buttonTouchRight } button2.x = 428.5 button2.y = 225 button2.xScale = 0.5 button2:setLabel(  )

and the optimization but i have an error “attempt to index field”

this method is incompatible with the widget? or I have do something wrong ?

local widget = require( "widget" ) --TEXT INFORMATION------------------------------------------------------------------------------------------------------------------------------------- local textInfo1 = display.newText("", 240,105,native.systemFont,100) textInfo1:setFillColor(1,0,0) isButtonClick = 0 --BOUTTON----------------------------------------------------------------------------------------------------------------------------------------- --Table to store the buttons local myButtons = {} myButtons[1] = widget.newButton myButtons[1].myId = "LeftButton"  --Set the buttons id myButtons[1].width = 240 myButtons[1].height = 120 myButtons[1].defaultFile = "buttoninactif.png" myButtons[1].overFile = "buttonpressed.png" myButtons[1].label = "buttonL" myButtons[1].x = 53 myButtons[1].y = 225 myButtons[1].xScale = 0.5 myButtons[1].setLabel = "Gauche" myButtons[2] = widget.newButton myButtons[2].myId = "RightButton"  --Set the buttons id myButtons[2].width = 240 myButtons[2].height = 120 myButtons[2].defaultFile = "buttoninactif.png" myButtons[2].overFile = "buttonpressed.png" myButtons[2].label = "buttonR" myButtons[2].x = 428 myButtons[2].y = 225 myButtons[2].xScale = 0.5 myButtons[2].setLabel = "Droit" --Function to handle our buttons local function handleButtons(event)      local target = event.target      --Handle action for each different button      if target.myId == "LeftButton" then             textInfo1.text = "(".."touchleft"..")"      elseif target.myId == "RightButton" then             textInfo1.text = "(".."touchright"..")"      end             return true end --Add event listeners for all the buttons for i = 1, #myButtons do     myButtons[i]:addEventListener("tap", handleButtons) end