Created
December 12, 2015 02:03
-
-
Save dduan/76cdee98624f0ef74bde to your computer and use it in GitHub Desktop.
A simple program to benchmark Set.intersectInPlace() in Swift
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
import Foundation | |
let smallSet = Set(Array(1..<999).sort {_, _ in arc4random() % 2 == 0}) | |
let largeSet = Set(Array(1..<99999).sort {_, _ in arc4random() % 2 == 0}) | |
var _count = 999999 | |
let unique: () -> Int = { _count += 1; return _count } | |
var before = NSDate() | |
var after = NSDate() | |
var smallLargeDuration: NSTimeInterval = 0 | |
(1...100).forEach { _ in | |
var a = smallSet | |
var b = largeSet | |
a.insert(unique()) | |
b.insert(unique()) | |
before = NSDate() | |
a.intersectInPlace(b) | |
after = NSDate() | |
smallLargeDuration += after.timeIntervalSinceDate(before) | |
} | |
print("small ∩ large * 100 -- \(smallLargeDuration)") | |
var largeSmallDuration: NSTimeInterval = 0 | |
(1...100).forEach { _ in | |
var b = smallSet | |
var a = largeSet | |
a.insert(unique()) | |
b.insert(unique()) | |
before = NSDate() | |
a.intersectInPlace(b) | |
after = NSDate() | |
largeSmallDuration += after.timeIntervalSinceDate(before) | |
} | |
print("large ∩ small * 100 -- \(largeSmallDuration)") | |
var smallSmallDuration: NSTimeInterval = 0 | |
(1...100).forEach { _ in | |
var b = smallSet | |
var a = smallSet | |
a.insert(unique()) | |
b.insert(unique()) | |
before = NSDate() | |
a.intersectInPlace(b) | |
after = NSDate() | |
smallSmallDuration += after.timeIntervalSinceDate(before) | |
} | |
print("small ∩ small * 100 -- \(smallSmallDuration)") | |
var largeLargeDuration: NSTimeInterval = 0 | |
(1...100).forEach { _ in | |
var b = largeSet | |
var a = largeSet | |
a.insert(unique()) | |
b.insert(unique()) | |
before = NSDate() | |
a.intersectInPlace(b) | |
after = NSDate() | |
largeLargeDuration += after.timeIntervalSinceDate(before) | |
} | |
print("large ∩ large * 100 -- \(largeLargeDuration)") | |
print("total time spent ----- \(smallLargeDuration + largeSmallDuration + smallSmallDuration + largeLargeDuration)") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment