I have an app with a couple of thousand lines and within that code there are a lot of println() commands. Does this slow the app down? It is obviously being executed in the Simulator, but what happens when you archive, submit and download the app from the app store/TestFlight. Is this code still "active", and what about code that is "commented out"?
Is it literally never read or should I delete commented out code when I submit to test flight/app store?
Yes it does slow the code.
Both print
and println
decrease performance of the application.
println
is not removed when Swift does code optimisation.
for i in 0...1_000 {
println(i)
}
This code can't be optimised and after compiling Assembly code would perform a loop with 1000 instructions that actually don't do anything valuable.
The problem is that Swift compiler can't do optimal optimisation to the code with print
and println
commands.
You can see it if you have a look on generated Assembly code.
You can do see assembly code with Hopper Disassembler or by compiling Swift code to the Assembly with by using swiftc
compiler:
xcrun swiftc -emit-assembly myCode.swift
Lets have a look on few examples for better understanding.
Swift compiler can eliminate a lot of unnecessary code like:
Example:
class Object {
func nothing() {
}
}
for i in 0...1_000 {
let object = Object3(x: i)
object.nothing()
object.nothing()
}
In this example Swift complier would do this optimisation:
1. Remove both nothing
method calls
After this the loop body would have only 1 instruction
for i in 0...1_000 {
let object = Object(x: i)
}
2. Then it would remove creating Object
instance, because it's actually not used.
for i in 0...1_000 {
}
3. The final step would be removing empty loop.
And we end up with no code to execute
print
and println
This is definitely not the best solution.//println("A")
DEBUG
preprocessor statementWith this solution you can simple change logic of your debug_print functiondebug_println("A)
func debug_println<T>(object: T) {
#if DEBUG
println(object)
#endif
}
Always Remove print
and println
from release application!!
If you add print
and println
instruction, the Swift code can't be optimised in the most optimal way and it could lead to the big performance penalties.
Generally you should not leave any form of logging turned on in a production app, it will most likely not impact performance but it is poor practice to leave it enabled and unneeded.
As for commented code, this is irrelevant as it will be ignored by the compiler and not be part of the final binary.
See this answer on how to disable println()
in production code, there is a variety of solutions, Remove println() for release version iOS Swift
As you do not want to have to comment out all your println()
calls just for a release, it is much better to just disable them, otherwise you'll be wasting a lot of time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With