Skip to content

Instantly share code, notes, and snippets.

@YusukeHosonuma
Created July 17, 2016 13:12
Show Gist options
  • Save YusukeHosonuma/e8a816bb48f549316114f3c1c7dfe56a to your computer and use it in GitHub Desktop.
Save YusukeHosonuma/e8a816bb48f549316114f3c1c7dfe56a to your computer and use it in GitHub Desktop.
[Swift] Measure Set's contains() performance
func time(title: String, execute: () -> ()) {
let now = NSDate()
execute()
let interval = NSDate().timeIntervalSinceDate(now)
print("\(title): \(interval)")
}
var s = Set<String>()
(0..<1000).forEach{ s.insert(String($0)) }
time("\(s.count) elements") {
_ = s.contains("1")
}
(1000..<10000).forEach{ s.insert(String($0)) }
time("\(s.count) elements") {
_ = s.contains("1")
}
(10000..<100000).forEach{ s.insert(String($0)) }
time("\(s.count) elements") {
_ = s.contains("1")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment