Skip to content

Instantly share code, notes, and snippets.

@dduan
Created December 12, 2015 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dduan/76cdee98624f0ef74bde to your computer and use it in GitHub Desktop.
Save dduan/76cdee98624f0ef74bde to your computer and use it in GitHub Desktop.
A simple program to benchmark Set.intersectInPlace() in Swift
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