Skip to content

Instantly share code, notes, and snippets.

@dennisvennink
Last active June 10, 2018 00:06
Show Gist options
  • Save dennisvennink/6dda0fad0305a09c474dc8449c34988a to your computer and use it in GitHub Desktop.
Save dennisvennink/6dda0fad0305a09c474dc8449c34988a to your computer and use it in GitHub Desktop.
import XCTest
@testable import SetUnion
final class SetUnionTests: XCTestCase {
let a1: [Set<Int>?] = [[1, 2], [2, 3], nil]
let n = 100000
func testX1 () {
self.measure {
for _ in 1...self.n {
_ = Set(self.a1.flatMap { $0 ?? [] })
}
}
}
func testX2 () {
self.measure {
for _ in 1...self.n {
_ = self.a1.reduce(Set<Int>()) { $0.union($1 ?? []) }
}
}
}
func testX3 () {
self.measure {
for _ in 1...self.n {
_ = self.a1.reduce(into: Set<Int>()) { $0.formUnion($1 ?? []) }
}
}
}
func testX4 () {
self.measure {
for _ in 1...self.n {
var x4 = Set<Int>()
for set in a1 {
x4.formUnion(set ?? [])
}
}
}
}
func testX5 () {
self.measure {
for _ in 1...self.n {
var x5 = Set<Int>()
a1.forEach { x5.formUnion($0 ?? []) }
}
}
}
func testX6 () {
self.measure {
for _ in 1...self.n {
_ = Set(a1.compactMap { $0 }.flatMap { $0 })
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment