반응형
2. Avoid Functions as Arguments for Other Functions
루프나 time-critical code 에서 함수를 localize 하는 것은 아주 중요합니다. 그러면 다른 함수들에 파라미터로 사용됩니다. 아래 두 경우를 보세요:
Defined as an Argument of Another Function — Discouraged
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
Localized — Recommended
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
3. Avoid “table.insert()”
아래 네 경우를 비교해 보죠. 4개 모두 결과는 같습니다. 테이블에 값을 insert 하는 일반적인 방법들입니다. 이 4가지 중 Lua 의 table.insert 함수를 사용하는것은 별로 좋지 않은 방법입니다.
table.insert() — Discouraged
local a = {}
local table_insert = table.insert
for i = 1,100 do
table_insert( a, i )
endLoop Index Method — Recommended
local a = {}
for i = 1,100 do
a[i] = i
endTable Size Method — Recommended
local a = {}
for i = 1,100 do
a[#a+1] = i
endCounter Method — Recommended
local a = {}
local index = 1
for i = 1,100 do
a[index] = i
index = index+1
end4. Minimize use of “unpack()”
Lua unpack() function 도 퍼포먼스 측면에서 그렇게 좋은 하수가 아닙니다. 다행히 같은 결과를 내기 위해 간단하고 빠르게 loop 를 사용할 수 있습니다.
Lua “unpack()” method — Discouraged
local a = { 100, 200, 300, 400 }
for i = 1,100 do
print( unpack(a) )
endLoop Method — Recommended
local a = { 100, 200, 300, 400 }
for i = 1,100 do
print( a[1],a[2],a[3],a[4] )
end5. Cache Table Item Access
테이블 아이템들을 캐싱하는 것. 특히 루프 내에서. 이 방법을 사용하면 퍼포먼스를 향상시키고 time-critical code를 만들 수 있습니다.
Non-Cached — Acceptable
for i = 1,100 do
for n = 1,100 do
a[n].x = a[n].x + 1
print( a[n].x )
end
endCached — Recommended
for i = 1,100 do
for n = 1,100 do
local y = a[n]
y.x = y.x + 1
print( y.x )
end
end반응형
'Corona SDK > Corona Doc' 카테고리의 다른 글
| Tutorial: Extending Libraries Without Native Code - 2/2 - (1) | 2013.07.08 |
|---|---|
| Tutorial: Extending Libraries Without Native Code - 1/2 - (0) | 2013.07.08 |
| 화면 전환시 오디오 파일 다루기 (0) | 2013.06.11 |
| 퍼포먼스 최적화(Performance Optimizations) 팁들 소개 -04- (1) | 2013.03.15 |
| 퍼포먼스 최적화(Performance Optimizations) 팁들 소개 -03- (0) | 2013.03.14 |
| 퍼포먼스 최적화(Performance Optimizations) 팁들 소개 -01- (0) | 2013.03.13 |
| Physics 트릭들 배우기 - Wind Tunnel- (0) | 2013.02.27 |
| Physics 트릭들 배우기 - Sticky Projectiles- (0) | 2013.02.27 |
| Physics 트릭들 배우기 - Can I Jump?- (0) | 2013.02.26 |
| iOS Build-in 트위터 기능 사용하기 (0) | 2013.02.20 |