Skip to content

Instantly share code, notes, and snippets.

@mattneub
Created December 4, 2016 00:16
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 mattneub/430fef70e3496f5ce6917aa35c98f419 to your computer and use it in GitHub Desktop.
Save mattneub/430fef70e3496f5ce6917aa35c98f419 to your computer and use it in GitHub Desktop.
Testing efficiency of Swift collision test as a Set is constructed
struct S : Hashable {
static func ==(lhs:S,rhs:S) -> Bool {
print("called == for", lhs.id, rhs.id)
return lhs.id == rhs.id
}
let id : Int
var hashValue : Int {
print("called hashValue for", self.id)
return self.id
}
init(_ id:Int) {self.id = id}
}
var s = Set<S>()
for i in 1...5 {
print("inserting", i)
s.insert(S(i))
}
/*
inserting 1
called hashValue for 1
inserting 2
called hashValue for 2
called == for 1 2
called hashValue for 1
called hashValue for 2
inserting 3
called hashValue for 3
inserting 4
called hashValue for 4
called == for 3 4
called == for 1 4
called hashValue for 2
called hashValue for 3
called hashValue for 1
called hashValue for 4
called == for 3 4
called == for 1 4
inserting 5
called hashValue for 5
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment