Last active
September 16, 2016 14:12
-
-
Save resuna/32f8184829d7726ea112c2d5ed8163f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParkBenchTimer { | |
let startTime:CFAbsoluteTime | |
var endTime:CFAbsoluteTime? | |
init() { | |
startTime = CFAbsoluteTimeGetCurrent() | |
} | |
func stop() -> CFAbsoluteTime { | |
endTime = CFAbsoluteTimeGetCurrent() | |
return duration! | |
} | |
var duration:CFAbsoluteTime? { | |
if let endTime = endTime { | |
return endTime - startTime | |
} else { | |
return nil | |
} | |
} | |
} | |
let t1 = ParkBenchTimer() | |
var i = 0 | |
var s: String = "" | |
for e in a { | |
s = e | |
i += 1 | |
} | |
print("Took \(t1.stop())s final \(i)\(s)") | |
let t2 = ParkBenchTimer() | |
a.forEach { | |
s = $0 | |
i += 1 | |
} | |
print("Took \(t2.stop())s final \(i)\(s)") | |
let t3 = ParkBenchTimer() | |
for e in a { | |
s = e | |
i += 1 | |
} | |
print("Took \(t3.stop())s final \(i)\(s)") | |
//Took 0.00463801622390747s final 100000100000 | |
//Took 0.0364949703216553s final 200000100000 | |
//Took 0.0037190318107605s final 300000100000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment