1、没有了亲切的C语言for循环,先上代码
var hhArr:Array =[1,2,423,12,123435,2,123,12,234,563,412,3,34,12,4,45,1234,6,23,41,35,4652,34,15,234,124,21,123,23,123,123,12,3,23,4,536,23,124,5,1,24,345,124,5,1234,45,723,4,124,234,124,12,3,123,123,234,4567,546,45,67,456]
var stratTime = CACurrentMediaTime()
//forin 遍历(只有value)
for num in hhArr {
print(num)
}
var duration:String = String(format: "%.7f", (CACurrentMediaTime() - stratTime))
print("forin执行时间\(duration) \n")
stratTime = CACurrentMediaTime()
//Range 遍历 (index,value)
for index in 0..<hhArr.count {
print("index:\(index),value:\(hhArr[index])");
}
duration = String(format: "%.7f", (CACurrentMediaTime() - stratTime))
print("Range执行时间\(duration) \n")
stratTime = CACurrentMediaTime()
//enmerated 遍历 (offset,value)
for num in hhArr.enumerated() {
let rs:String = "index:\(num.offset) value:\(num.element)";
print(rs);
}
duration = String(format: "%.7f", (CACurrentMediaTime() - stratTime))
print("enmerated执行时间\(duration) \n")
stratTime = CACurrentMediaTime()
//enmerated 赋值遍历 (index,value)
for (index,value) in hhArr.enumerated() {
print("index:\(index),value:\(value)");
}
duration = String(format: "%.7f", (CACurrentMediaTime() - stratTime))
print("enmerated赋值执行时间\(duration) \n")
stratTime = CACurrentMediaTime()
//enmerated 反向遍历 (index,value)
for (index,value) in hhArr.enumerated().reversed() {
print("index:\(index),value:\(value)");
}
duration = String(format: "%.7f", (CACurrentMediaTime() - stratTime))
print("enmerated倒序执行时间\(duration) \n")
stratTime = CACurrentMediaTime()
2、统计分析
3、结论
Range > enmerated赋值遍历 > enmerated倒序 > enmerated遍历value > forin 推荐大家使用swift 亲儿子 Range,也就是... 或者..<