[Tips] Optimization 101

Here´s a couple of functions I made to both check what´s actually going on inside the package.loaded table at runtime:

I make a global function in the mainfile:

[LUA]

function printClasses()
for k,v in pairs(_G.package.loaded) do
  print(k)
end
end

function deleteClass(class)
if package.loaded[class] then
  package.loaded[class] = nil
  print("**** Removed class: " … class, " ******")
end
end

[/LUA]

If you run your code with theese functions inside you can look in the terminal and se all the modules/ classes loaded.

Now, if you have modules loaded in your scripts like i.e:

[LUA]

require(“MyModule”)

[/LUA]

This will be listed in the terminal window if you call:

[lua]

printClasses()

[/LUA]

Now, let´s say that you want to clean up because the player has won or lost and he/ she is taken to a typical gameover page you can run the second function and remove the module completely, you just call the function from whereever you like in your code:

[LUA]

deleteClass(“MyModule”)

[/LUA]

The simulator will print out: **** Removed class: MyModule  ******

I use these functions alot

Thanks a lot Hendrix000007 ! It’s gonna be very usefull to me :slight_smile:

https://docs.coronalabs.com/guide/basics/optimization/index.html

I noticed the localizing parts refer to the main lua libraries (math, io, type),  

1st question: Would this also prove useful to the corona sdk libraries… such as if i was creating a lot of display.newText’s in a file,  would be better or worse if I was to declare local dNewText = display.newText or would there be no noticeable difference? 
 

2nd question: I am also assuming that all these core lua functions are ones I should be localize for optimization if I use them in my code.

On the bottom of the manual
http://www.lua.org/manual/5.1/

I am trying to find the find the line between optimization and over optimization.

@noribl87,

  1. Localization would make it faster, but not noticeably unless you did many.

  2. Localization gives you the most speedup for functions where, the ‘table lookup’ is comparable to the execution time of the function.

(T below is a ‘made up unit of time’ for the examples just to show relative improvement)

Example 1 -  Short Execution Time:

  • ‘table lookup’ is 2T
  • function execution in 10T
  • total cost to call == 12T

Localized lookup:

  • localized lookup is 1T 
  • function execution in 10T
  • total cost to call == 11T
  • 9% improvement

Example 2 - Long Execution time

  • ‘table lookup’ is 2T 
  • function execution in 100T
  • total cost to call == 102T

Localized lookup:

  • localized lookup is 1T 
  • function execution in 100T
  • total cost to call == 101T
  • 1% improvement

Generally, I use localization for two reasons:

  • Speedup
  • Ease of typing (more valuable for your example)

So in your example, I’d simply do this:

local newText = display.newText -- some speedup, way faster to type.

Makes sense,  I’m assuming the corona libraries are optimized enough so they are not as long with lookup times vs calling a core lua library  :).  But yeah, I’ll experiment around and see if there is any negligible improvement.

thanks

Can’t argue too much with Ed’s logic in particular that newText is easier to type that display.newText. The one thing is supportability. I was just looking at a project for someone else and they had abstracted things out and it made it tougher to read.

But on software optimization, you need to decide how often you are calling an API call. For instance, if you only need one random number at the beginning of a scene:create() event, The few microseconds you would gain isn’t really perceivable. Our modern devices execute 10’s of millions of instructions per second.

Now lets say you’re generating a curve and need to call a lot of sin(), cos() and other trig functions in a tight loop, then those few microseconds add up in a hurry.

Please help me in optimizing the code below , it hangs in few seconds.-


– main.lua


– Your code here

local physics = require “physics”

physics.start()

physics.setGravity( 0, 0)

local  math = require(“math”)

display.setStatusBar(display.HiddenStatusBar) 

local outermainarray={}

local h2=820;

local dotsforshootarray = {}

local bulletanglearray={}

local dotarray={}

local pausedcircle11

local pausedcircle

local id=1;

local DX, DY = 3, 3

local XMIN, XMAX = 1, display.contentWidth-1

local YMIN, YMAX = 20, display.contentHeight-1

 _W = display.contentWidth; – Get the width of the screen

 _H = display.contentHeight; – Get the height of the screen

 motionx = 0; – Variable used to move character along x axis

 speed = 2; – Set Walking Speed

local w = display.contentWidth

local h = display.contentHeight

local centerX = display.contentCenterX

local centerY = display.contentCenterY

local myTable = {}

local rect

local Cos = math.cos

local Sin = math.sin

local Rad = math.rad

local Atan2 = math.atan2

local Deg = math.deg 

local function arc(x, y, radius, theta1, theta2, width, detail)

local vertices = {};

local k = 1;


– UPRIGHT - Comment out UPSIDE DOWN if using


for i = (360 - theta2), (360 - theta1), detail

do

vertices[k] = radius*math.cos(2*math.pi*(i / 360));

vertices[k + 1] = radius*math.sin(2*math.pi*(i / 360));

k = k + 2;

end

for i = ((360 - theta1) - ((360 - theta1) % detail)), (360 - theta2), -detail

do

vertices[k] = (radius - width)*math.cos(2*math.pi*(i / 360));

vertices[k + 1] = (radius - width)*math.sin(2*math.pi*(i / 360));

k = k + 2;

end

return display.newPolygon(x, y, vertices), vertices;

end

local function spawnnewloop( event )

  

          

          

print(myTable)

            local function spawnnewrings(event)

 

                  

for s=#myTable,1,-1 do

      

if myTable[s]~=nil   then

if myTable[s].y>1200 then

transition.cancel( myTable[s] )

display.remove(myTable[s])

  myTable[s] = nil

  end

end

end

                       

if #myTable<200 then

           spawnnew()

 end

              end

for j=#myTable,1,-1 do

if(myTable[j]~=nil) then

          transition.to(myTable[j],{time=2000, y=myTable[j].y+1200,onComplete=spawnnewrings})

  end

  end

            

                 

                

         

            

    

end

function spawnBubble(ringx,ringy,ringradi,ringstart,ringend,bubblex,bubbley,rotationspeed)

– add bottom arch 2

  

       local circle2bubble =display.newCircle(display.contentWidth/2,display.contentHeight/2,25)

 circle2bubble:setFillColor( 0, 1, 1 )

circle2bubble.x = bubblex

circle2bubble.y = bubbley-364

circle2bubble.linearDamping=0

circle2bubble.myName=“bubble”

physics.addBody(circle2bubble)

 transition.to(circle2bubble,{time=1000, y=bubbley,x=bubblex})

 circle2bubble.isSensor = true

 myTable[#myTable+1]=circle2bubble

 transition.to(circle2,{time=1000, y=ringy,x=ringx})

end

function spawnnew( event )

if (myTable[#myTable]~=nil) then

h2=myTable[#myTable].y-200

end

for m = 1, 5 do

if(m%5==1) then

spawnBubble(384,h2, 2, -95,2,384,h2,6.5)

end

if(m%5==2)  then

spawnBubble(160,h2, 2, -95,2,160,h2,6.5)

end

if(m%5==3)  then

spawnBubble(520, h2, 2, -95,2,520,h2,6.5)

end

if(m%5==4)  then

spawnBubble(190, h2, 2, -95,2,190,h2,6.5)

end

if(m%5==0)  then

spawnBubble(500, h2, 2, -95,2,500,h2,6.5)

spawnnewloop()

–h2=h2+700

end

h2=h2-200

end

end

spawnnew()

Hiiiiiii.
This  information is  a  best  and  funtastic  info…i liked this  info  i  want  more   info this easily types…so you create  more  topics  so you  add me  b cose  i liked very liked this topics