//: Playground - noun: a place where people can play
// FP-KVS版
//import UIKit
import Foundation
var str = "Hello, playground"
let MAX = 100//_000
let testValues = Array(count:MAX, repeatedValue:2)
var total = 0, startTime=0.0
func reset() {
total = 0
startTime = currentTime()
}
func currentTime() -> Double {
return ((CFAbsoluteTimeGetCurrent() as Double) * 1000.0)
}
func letUsLoopin(@noescape f:()->Void) -> Double {
reset()
f()
return (currentTime() - startTime)
}
// 実行関数配列
var runners :[String:()->Void] = [
"closed for": {
for i in 0 ..< testValues.count {
total += testValues[i]
}
},
"opened for" : {
let max = testValues.count - 1
for i in 0...max {
total += testValues[i]
}
},
"zipIndexing" : {
for (index,v) in testValues.enumerate() {
total += v
}
},
"for-in" : {
for v in testValues {
total += v
}
},
"for-each" : {
testValues.forEach {
total += $0
}
},
"while" : {
var test = testValues.count - 1
while test > 0 {
total += testValues[test]
test -= 1
}
},
"do-while" : {
var i = 0
repeat {
total += testValues[i]
i += 1
} while(i < testValues.count)
},
]
// 実行時間記録
var runTimes = runners.map { ($0.0, Double(0)) }
let trialTimes = 1_000_000
for i in 0 ..< trialTimes {
runTimes = runTimes.map {
($0.0, $0.1 + letUsLoopin(runners[$0.0]!))
}
}
runTimes.forEach {
print("'\($0.0)':\($0.1),")
}