Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
(surprising) speed test: (global) integer constants vs strings as constants — Gideros Forum

(surprising) speed test: (global) integer constants vs strings as constants

keszeghkeszegh Member
edited April 2014 in Game & application design
following other programming languages' conventions i thought that maybe i should change my string constants to integer values to speed up the code. by that i mean that an object of mine has several states, so (self.)state has value "STRING1" or "STRING2" or etc. and i changed these so that i defined global STRING1=1, STRING2=2 etc. and used these. suprisingly the code became slower. making these constants local sped up the things, a bit faster than with strings, but still only approx. to the same speed as the string solution.
it is a bit surprising for me, and the conclusion is that in lua you don't need to optimize this (as many other things, e.g. sin tables).

summary:
constants vs local constants vs strings
pc:
2.1765020517934 vs 1.2641564022966 vs 1.2715337009151
android:
6.2562990000006 vs 4.3186989999958 vs 4.6480369999917

test code:
 
TOUCHED=1
RELEASED=2
 
something=TOUCHED
startTime=os.timer()
for i = 1, 10000000 do
	if something==TOUCHED then something=RELEASED 
	elseif something==RELEASED then something=TOUCHED end
end
print(os.timer()-startTime)
 
local TOUCHED=1
local RELEASED=2
 
something=TOUCHED
startTime=os.timer()
for i = 1, 10000000 do
	if something==TOUCHED then something=RELEASED 
	elseif something==RELEASED then something=TOUCHED end
end
print(os.timer()-startTime)
 
something="TOUCHED"
startTime=os.timer()
for i = 1, 10000000 do
	if something=="TOUCHED" then something="RELEASED"
	elseif something=="RELEASED" then something="TOUCHED" end
end
print(os.timer()-startTime)

Comments

Sign In or Register to comment.