Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does print() / println() slow execution?

Tags:

ios

swift

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?

like image 634
KML Avatar asked Feb 26 '15 09:02

KML


2 Answers

Yes it does slow the code.
Both print and println decrease performance of the application.

Println problem

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.

Analysing Assembly code

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

Swift code optimisation

Lets have a look on few examples for better understanding.
Swift compiler can eliminate a lot of unnecessary code like:

  • Empty function calls
  • Creating objects that are not used
  • Empty Loops

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

Solutions

  • Comment out print and println

This is definitely not the best solution.
//println("A")

  • Use DEBUG preprocessor statement

With this solution you can simple change logic of your debug_print function
debug_println("A)

func debug_println<T>(object: T) {
  #if DEBUG
    println(object)
  #endif
}

Conclusion

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.

like image 87
Kostiantyn Koval Avatar answered Nov 16 '22 05:11

Kostiantyn Koval


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.

like image 37
Tim Avatar answered Nov 16 '22 04:11

Tim