这是最基础也是最有用的策略,虽然使用全局变量并不能彻底避免,但仍是应该尽可能避免,取而代之使用局部变量即local
。这里的局部变量也包括函数function
,由于在Lua里函数也是一个变量。局部变量的存取会更快,并且在生命周期以外就会释放掉。html
使用全局变量web
CCX = display.contentCenterX --global variable for i = 1,100 do local image = display.newImage( "myImage" ) image.x = CCX end |
使用局部变量api
local CCX = display.contentCenterX --local variable for i = 1,100 do local image = display.newImage( "myImage" ) image.x = CCX end |
这个原则也适用于Lua的核心库,好比math
库。对于这类函数也应该使用局部变量。缓存
非局部变量app
local function foo( x ) for i = 1,100 do x = x + math.sin(i) end return x end |
使用局部变量函数
local sin = math.sin --local reference to math.sin local function foo(x) for i = 1,100 do x = x + sin(i) end return x end |
最后,记住尽可能用局部变量的方式来使用函数。固然,这样须要注意函数的做用域的问题。若是你对Lua的做用域还不够清楚,请看Understanding “SCOPE” for beginning programmers。性能
使用全局的函数优化
function func1() func2( "myValue" ) end function func2( y ) print( y ) end func1() |
使用局部的函数lua
--"func2" properly scoped above "func1" local function func2( y ) print( y ) end local function func1() func2( "myValue" ) end func1() |
若是函数的参数是函数时,应将参数函数做为局部变量传进参数,而不要直接写函数定义,请看下面两个例子:spa
直接在参数表里定义函数
local func1 = function(a,b,func) return func(a+b) end for i = 1,100 do local x = func1( 1, 2, function(a) return a*2 end ) print( x ) end |
使用局部变量传参
local func1 = function( a, b, func ) return func( a+b ) end local func2 = function( c ) return c*2 end for i = 1,100 do local x = func1( 1, 2, func2 ) print( x ) end |
table.insert()
下面来看看4个实现表插入的方法。在4个方法之中table.insert()
在效率上不如其余方法,是应该避免使用的。
使用table.insert
local a = {} local table_insert = table.insert for i = 1,100 do table_insert( a, i ) end |
使用循环的计数
local a = {} for i = 1,100 do a[i] = i end |
使用table的size
local a = {} for i = 1,100 do a[#a+1] = i end |
使用计数器
local a = {} local index = 1 for i = 1,100 do a[index] = i index = index+1 end |
unpack()
函数Lua的unpack()函数不是一个效率很高的函数。你彻底能够写一个循环来代替它的做用。
使用unpack()
local a = { 100, 200, 300, 400 } for i = 1,100 do print( unpack(a) ) end |
代替方法
local a = { 100, 200, 300, 400 } for i = 1,100 do print( a[1],a[2],a[3],a[4] ) end |
缓存table的元素,特别是在循环内使用会提升效率。
未缓存
for i = 1,100 do for n = 1,100 do a[n].x = a[n].x + 1 print( a[n].x ) end end |
缓存
for i = 1,100 do for n = 1,100 do local y = a[n] y.x = y.x + 1 print( y.x ) end end |
ipairs()
当遍历table时,使用Lua的ipairs()的效率并不高。
使用ipairs()
local t1 = {} local t2 = {} local t3 = {} local t4 = {} local a = { t1, t2, t3, t4 } for i,v in ipairs( a ) do print( i,v ) end |
代替方法
local t1 = {} local t2 = {} local t3 = {} local t4 = {} local a = { t1, t2, t3, t4 } for i = 1,#a do print( a[i] ) end |
应该使用更快的数学方法。
math.fmod()
--math.fmod method (discouraged) local fmod = math.fmod for i = 1,100 do if ( fmod( i,30 ) < 1 ) then local x = 1 end end --modulus operator method (recommended) for i = 1,100 do if ( ( i%30 ) < 1 ) then local x = 1 end end |
x * 0.5 ; x * 0.125 --recommended x/2 ; x/8 --discouraged |
x * x * x --recommended x^3 --discouraged |
Texture内存通常只有在出现警告时才会去关注,但那时就很难去补救了。
若是你要在场景中使用必定数量的物理对象,那么预先建立全部的对象会在使用时提升效率。那些暂时用不到的对象能够设置为未激活的状态而后放到屏幕的外面或者放到一个不可见的group里。当须要时设置对于的位置并激活便可。 实时建立物理对象并非不能够,但一次性建立10-20个对象必然会形成性能问题,而致使顿卡延时等。 固然,也要考虑到内存的问题,若是一开始就建立几百个物体,那么对于内存的消耗就使得性能提升有点得不偿失了。
有些音效是整个app都会用到的,这样的音效须要一开始就载入到内存中。对于音质并无特别高的要求的音乐和音效须要考虑是否须要压缩,好比使用11khz来减少音频文件的大小,通常用户也听不出太大的区别,而这样减小的内存但是至关可观的。并且要使用简单的适合多平台的音频格式,好比WAV格式。 若是须要的话,音效能够以下组织成一个table,这样便于在使用时引用或者在不须要的时候释放掉。
--load these sounds during NON-time-critical code local soundTable = { mySound1 = audio.loadSound( "a.wav" ), mySound2 = audio.loadSound( "b.wav" ), mySound3 = audio.loadSound( "c.wav" ), mySound4 = audio.loadSound( "d.wav" ), mySound5 = audio.loadSound( "e.wav" ), mySound6 = audio.loadSound( "f.wav" ), mySound7 = audio.loadSound( "g.wav" ), mySound8 = audio.loadSound( "h.wav" ), } |
播放一个音效就很是简单:
local mySound = audio.play( soundTable["mySound1"] ) |
永远不要忘记,当音效不须要的时候就要释放掉:
local ST = soundTable for s,v in pairs(ST) do audio.dispose( ST[s] ) ; ST[s] = nil end |