I have a lot of places in my app calling print()
Would print() slow down app performance?
Should I turn it off when publishing?
Thanks.
I have a lot of places in my app calling print()
Would print() slow down app performance?
Should I turn it off when publishing?
Thanks.
Yes - simple way is to override the print function, like so:
function print()
end
However it would probably be preferable to have a variable that dictates whether you’re in production or develop mode:
[lua]
local debug = false
if debug == false then
function print()
end
[/lua]
By toggling this variable you can then have the prints turned off for production and vice-versa.
WARNING!!!
YOU NEVER OVERRIDE ‘debug’ VARIABLE!
It will override Corona debug function and will make your app UNABLE to print ANY error which will occur!
What you want to do is
[lua]
– in main.lua for example
local appMode = “debug”
if appMode == “debug” then
oldPrint = print
print = function() end
end
[/lua]
I have used globals here on purpose to override global print function (don’t do it in other cases). What you do, is replace print with empty function. When you will call print() next time this function (which does nothing) will be called.What’s more, it is always good idea to keep original function somewhere.
You better don’t override any of this until you really know what you are doing (else Corona will start to behave strangely or in unexpected way or even crash)
\_G =\> table: 0x10d510f90 \_VERSION =\> Lua 5.1 \_credits\_init =\> function: 0x108647cf0 \_network\_pathForFile =\> function: 0x101394180 al =\> table: 0x10868cc50 assert =\> function: 0x10868f3a0 audio =\> table: 0x108635da0 collectgarbage =\> function: 0x10138e7f0 coronabaselib =\> table: 0x10863b5f0 coroutine =\> table: 0x1086903b0 debug =\> table: 0x10136f730 display =\> table: 0x1013eb150 dofile =\> function: 0x10865e510 easing =\> table: 0x10138b600 error =\> function: 0x10865e540 gcinfo =\> function: 0x10865e570 getfenv =\> function: 0x1086a7f50 getmetatable =\> function: 0x10134d250 graphics =\> table: 0x108675420 io =\> table: 0x1013ef390 ipairs =\> function: 0x1013d2aa0 load =\> function: 0x10868fc90 loadfile =\> function: 0x10868fc30 loadstring =\> function: 0x1086b2ab0 lpeg =\> table: 0x10862c4d0 math =\> table: 0x10864a840 media =\> table: 0x1013d1a20 metatable =\> table: 0x10868caf0 module =\> function: 0x1013e0fd0 native =\> table: 0x1086559b0 network =\> table: 0x101393b40 newproxy =\> function: 0x108617180 next =\> function: 0x1013ec010 os =\> table: 0x10131c900 package =\> table: 0x10863fc10 pairs =\> function: 0x1013d9870 pcall =\> function: 0x1013ec040 print =\> function: 0x10863b660 rawequal =\> function: 0x1013c79a0 rawget =\> function: 0x1013c7a00 rawset =\> function: 0x1013e7ab0 require =\> function: 0x10138fbc0 select =\> function: 0x1013e7ae0 setfenv =\> function: 0x1013e7b10 setmetatable =\> function: 0x10134a3f0 string =\> table: 0x101311df0 system =\> table: 0x1013b2e60 table =\> table: 0x1013ad550 timer =\> table: 0x10138a550 tonumber =\> function: 0x10134a450 tostring =\> function: 0x1086067f0 transition =\> table: 0x10138e000 type =\> function: 0x10130ffb0 unpack =\> function: 0x10130ffe0 xpcall =\> function: 0x101310010
Good catch - my bad, normally I use a variable such as debugMode.
So print() indeed affect the performance if it’s not turned off?
Let’s say this that way - when you print in simulator printing and displaying on console for sure makes apps slower (I’m not talkng about extremes but slightly). On device, printed strings also must be streamed somewhere and saved (can compare it to writing to file). So for sure there will be some increase in speed but it usually will have really small penalty you won’t notice. However, ‘turning off’ functions always will make app at least a tiny bit faster (this way or other you are reducing calls to functions and it’s execution)
Thanks a lot
Yes - simple way is to override the print function, like so:
function print()
end
However it would probably be preferable to have a variable that dictates whether you’re in production or develop mode:
[lua]
local debug = false
if debug == false then
function print()
end
[/lua]
By toggling this variable you can then have the prints turned off for production and vice-versa.
WARNING!!!
YOU NEVER OVERRIDE ‘debug’ VARIABLE!
It will override Corona debug function and will make your app UNABLE to print ANY error which will occur!
What you want to do is
[lua]
– in main.lua for example
local appMode = “debug”
if appMode == “debug” then
oldPrint = print
print = function() end
end
[/lua]
I have used globals here on purpose to override global print function (don’t do it in other cases). What you do, is replace print with empty function. When you will call print() next time this function (which does nothing) will be called.What’s more, it is always good idea to keep original function somewhere.
You better don’t override any of this until you really know what you are doing (else Corona will start to behave strangely or in unexpected way or even crash)
\_G =\> table: 0x10d510f90 \_VERSION =\> Lua 5.1 \_credits\_init =\> function: 0x108647cf0 \_network\_pathForFile =\> function: 0x101394180 al =\> table: 0x10868cc50 assert =\> function: 0x10868f3a0 audio =\> table: 0x108635da0 collectgarbage =\> function: 0x10138e7f0 coronabaselib =\> table: 0x10863b5f0 coroutine =\> table: 0x1086903b0 debug =\> table: 0x10136f730 display =\> table: 0x1013eb150 dofile =\> function: 0x10865e510 easing =\> table: 0x10138b600 error =\> function: 0x10865e540 gcinfo =\> function: 0x10865e570 getfenv =\> function: 0x1086a7f50 getmetatable =\> function: 0x10134d250 graphics =\> table: 0x108675420 io =\> table: 0x1013ef390 ipairs =\> function: 0x1013d2aa0 load =\> function: 0x10868fc90 loadfile =\> function: 0x10868fc30 loadstring =\> function: 0x1086b2ab0 lpeg =\> table: 0x10862c4d0 math =\> table: 0x10864a840 media =\> table: 0x1013d1a20 metatable =\> table: 0x10868caf0 module =\> function: 0x1013e0fd0 native =\> table: 0x1086559b0 network =\> table: 0x101393b40 newproxy =\> function: 0x108617180 next =\> function: 0x1013ec010 os =\> table: 0x10131c900 package =\> table: 0x10863fc10 pairs =\> function: 0x1013d9870 pcall =\> function: 0x1013ec040 print =\> function: 0x10863b660 rawequal =\> function: 0x1013c79a0 rawget =\> function: 0x1013c7a00 rawset =\> function: 0x1013e7ab0 require =\> function: 0x10138fbc0 select =\> function: 0x1013e7ae0 setfenv =\> function: 0x1013e7b10 setmetatable =\> function: 0x10134a3f0 string =\> table: 0x101311df0 system =\> table: 0x1013b2e60 table =\> table: 0x1013ad550 timer =\> table: 0x10138a550 tonumber =\> function: 0x10134a450 tostring =\> function: 0x1086067f0 transition =\> table: 0x10138e000 type =\> function: 0x10130ffb0 unpack =\> function: 0x10130ffe0 xpcall =\> function: 0x101310010
Good catch - my bad, normally I use a variable such as debugMode.
So print() indeed affect the performance if it’s not turned off?
Let’s say this that way - when you print in simulator printing and displaying on console for sure makes apps slower (I’m not talkng about extremes but slightly). On device, printed strings also must be streamed somewhere and saved (can compare it to writing to file). So for sure there will be some increase in speed but it usually will have really small penalty you won’t notice. However, ‘turning off’ functions always will make app at least a tiny bit faster (this way or other you are reducing calls to functions and it’s execution)
Thanks a lot