Skip to content

Instantly share code, notes, and snippets.

@resuna
Last active September 16, 2016 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resuna/32f8184829d7726ea112c2d5ed8163f1 to your computer and use it in GitHub Desktop.
Save resuna/32f8184829d7726ea112c2d5ed8163f1 to your computer and use it in GitHub Desktop.
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