Created
January 30, 2018 09:01
-
-
Save rizumita/e5eda5299b532f688d9594348e0a2126 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
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