为什么for-in比在快速调试模式下慢? [英] Why is for-in slower than while in swift debugging mode?
问题描述
为什么for-in比在快速调试模式下慢?如果您认为,是的,它是没有优化的.
⬇️下面的代码,时间是for-in的比较,而没有进行优化
49999995000000换时间-时间= 3.3352
4999999950000000而-时间= 0.3613
⬇️但是,如果使用优化速度
49999995000000换时-时间= 0.0037
49999995000000而-时间= 0.0035
我想知道为什么for-in比没有优化时要慢?为什么在优化中如此之快?"
import Foundationfunc processTime(_ title:String,blockFunction:()->()){打印()让startTime = CFAbsoluteTimeGetCurrent()blockFunction()让processTime = CFAbsoluteTimeGetCurrent()-startTimeprint(title,-time = \(String(format:"%.4f,processTime))")}processTime("for-in"){var sum = 0对于i in 0 .. <10000000 {总和==我}打印(总和)}processTime("while"){var sum = 0变量i = 0而我<10000000 {总和==我我+ = 1}打印(总和)}
从Swift的角度来看,您的 for
循环实际上转换为如下形式:
让范围= 0 .. <10000000var iterator = range.makeIterator()而让next = iterator.next(){...}
请注意,该范围的迭代器上有很多对 next
的调用,必须跟踪其自身的状态,并且 IndexingIterator.next
会调用一堆协议方法,调度也需要一些时间,因为它必须查找见证表.确切查看 Iterator.next
的调用将使
80%的时间都花在 next()
上,看看它能做多少事情!我的屏幕截图甚至无法包含所有内容.串联的字符串仅占6%(屏幕截图中没有).
Why is for-in slower than while in swift debugging mode ? If you think, yes It was ran in no optimization.
⬇️Below code, Time is compare of for-in and while in no optimization
49999995000000 for-in -- time = 3.3352
4999999950000000 while -- time = 0.3613
⬇️but, If using optimization for speed
49999995000000 for-in -- time = 0.0037
49999995000000 while -- time = 0.0035
I wonder that "why is for-in slower than while in no optimization? and why are for-in and while so fast in optimization? "
import Foundation
func processTime(_ title: String, blockFunction: () -> ()) {
print()
let startTime = CFAbsoluteTimeGetCurrent()
blockFunction()
let processTime = CFAbsoluteTimeGetCurrent() - startTime
print(title, " -- time = \(String(format : "%.4f",processTime))")
}
processTime("for-in") {
var sum = 0
for i in 0..<10000000 {
sum += i
}
print(sum)
}
processTime("while") {
var sum = 0
var i = 0
while i<10000000 {
sum += i
i += 1
}
print(sum)
}
From a Swift perspective, your for
loop actually translates to something like this:
let range = 0..<10000000
var iterator = range.makeIterator()
while let next = iterator.next() {
...
}
Notice that's a lot of calls to next
on the range's iterator, which has its own state that has to be kept track of, and IndexingIterator.next
calls a bunch of protocol methods, dispatching which takes some time too as it has to lookup the witness table. See exactly what calls Iterator.next
is going to make here.
If you are in debug mode, none of that is going to be optimised.
Compare that to your while loop, which is basically set something to 0, compare, do the thing in the loop, add 1 to it, repeat. Clearly this is much simpler to do than calling all those methods.
If you enable optimisations however, the compiler can see that the for loop is doing what the while loop does anyway.
Because I find it interesting, I did a little time profile of a loop such as:
var s = ""
for i in 0...10000000 {
s += "\(i)"
}
80% of the time is spent on next()
, and look at how many things it does! My screenshot can't even contain everything. The string concatenating only takes up about 6% (not in the screenshot).
这篇关于为什么for-in比在快速调试模式下慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!