Skip to content

Instantly share code, notes, and snippets.

@rizumita
Created January 30, 2018 09:01
Show Gist options
  • Save rizumita/e5eda5299b532f688d9594348e0a2126 to your computer and use it in GitHub Desktop.
Save rizumita/e5eda5299b532f688d9594348e0a2126 to your computer and use it in GitHub Desktop.
func runSpeedTests() {
let s: String? = "test"
let repeatCount = 10000000000
var i = 0
test("Optional Binding") {
for _ in 0...repeatCount {
if let _ = s { i += 1 }
}
}
print(i)
var j = 0
test("eq not nil") {
for _ in 0...repeatCount {
if s != nil { j += 1 }
}
}
print(j)
var k = 0
test("switch") {
for _ in 0...repeatCount {
switch s {
case .some(_): k += 1
case .none: ()
}
}
}
print(k)
}
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