print()/println()是否会减慢执行速度? [英] Does print() / println() slow execution?
问题描述
我有一个包含数千行的应用程序,并且在该代码中有许多println()命令.这会降低应用速度吗?它显然是在模拟器中执行的,但是当您从应用程序商店/TestFlight存档,提交和下载应用程序时会发生什么.该代码是否仍然处于活动状态",被注释掉"的代码该如何处理?
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?
推荐答案
是的,它确实会使代码变慢.
print
和println
都会降低应用程序的性能.
Yes it does slow the code.
Both print
and println
decrease performance of the application.
println
.
for i in 0...1_000 {
println(i)
}
该代码无法优化,并且在汇编后,汇编代码将执行包含1000条指令的循环,而这些指令实际上并没有做任何有价值的事情.
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.
问题是Swift编译器无法使用print
和println
命令对代码进行最佳优化.
如果您查看生成的汇编代码,就可以看到它.
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.
您可以使用 Hopper 反汇编程序或通过使用
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编译器可以消除很多不必要的代码,例如:
Swift code optimisation
Lets have a look on few examples for better understanding.
Swift compiler can eliminate a lot of unnecessary code like:
- 空函数调用
- 创建不使用的对象
- 空循环
示例:
class Object {
func nothing() {
}
}
for i in 0...1_000 {
let object = Object3(x: i)
object.nothing()
object.nothing()
}
在此示例中,Swift编译器将进行以下优化:
In this example Swift complier would do this optimisation:
1..删除两个nothing
方法调用
此后,循环主体将只有1条指令
After this the loop body would have only 1 instruction
for i in 0...1_000 {
let object = Object(x: i)
}
2..这将删除创建的Object
实例,因为它实际上并未使用.
2. Then it would remove creating Object
instance, because it's actually not used.
for i in 0...1_000 {
}
3..最后一步是删除空循环.
最后,我们没有执行代码
3. The final step would be removing empty loop.
And we end up with no code to execute
- 注释
print
和println
这绝对不是最佳解决方案.
//println("A")
This is definitely not the best solution.
//println("A")
- 使用
DEBUG
预处理程序语句
- Use
DEBUG
preprocessor statement
使用此解决方案,您可以简单地更改debug_print函数的逻辑
debug_println("A)
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
}
结论
始终从发布应用程序中删除print
和println
!
如果添加print
和println
指令,则无法以最佳方式优化Swift代码,这可能会导致较大的性能损失.
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.
这篇关于print()/println()是否会减慢执行速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!