Created
July 17, 2016 13:12
-
-
Save YusukeHosonuma/e8a816bb48f549316114f3c1c7dfe56a to your computer and use it in GitHub Desktop.
[Swift] Measure Set's contains() performance
This file contains hidden or 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 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