Skip to content

Instantly share code, notes, and snippets.

@rizumita
Created January 30, 2018 08:49
Show Gist options
  • Save rizumita/11e76bad3a563b53ef10408debb39a37 to your computer and use it in GitHub Desktop.
Save rizumita/11e76bad3a563b53ef10408debb39a37 to your computer and use it in GitHub Desktop.
func runSpeedTests() {
let s: String? = "test"
let repeatCount = 100000000
test("Optional Binding") {
for _ in 0...repeatCount {
if let _ = s {}
}
}
test("eq not nil") {
for _ in 0...repeatCount {
if s != nil {}
}
}
test("switch") {
for _ in 0...repeatCount {
switch s {
case .some(_): ()
case .none: ()
}
}
}
}
func test(_ name: String, _ t: () -> ()) {
let methodStart = Date()
t()
let methodFinish = Date()
let executionTime = methodFinish.timeIntervalSince(methodStart)
print("Execution time - \(name): \(executionTime)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment