Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Last active December 9, 2016 01:37
Show Gist options
  • Save aleclarson/be3afc3ba677e313a570 to your computer and use it in GitHub Desktop.
Save aleclarson/be3afc3ba677e313a570 to your computer and use it in GitHub Desktop.
Convenience functions for ObjectIdentifier hashes
/// Generate a unique identifier for an AnyObject.
/// 2nd slowest. Converts to String.
public func obid (object: AnyObject) -> String {
return "\(obid(object) as Int)"
}
/// Generate a unique identifier for an AnyObject.
/// Fastest. Every other function relies on this one.
public func obid (object: AnyObject) -> Int {
return ObjectIdentifier(object).hashValue
}
/// Generate a unique identifier for an AnyObject.
/// Slowest. Checks for nil and converts to String.
public func obid (object: AnyObject!) -> String {
return "\(obid(object) as Int)"
}
/// Generate a unique identifier for an AnyObject.
/// 2nd fastest. Checks for nil.
public func obid (object: AnyObject!) -> Int {
return object != nil ? obid(object!) : 0
}
func measure (n: Int, fn: () -> ()) {
// return
let start = CFAbsoluteTimeGetCurrent()
for i in 0...n {
fn()
}
println(CFAbsoluteTimeGetCurrent() - start)
}
let n = 100
let view = UIView()
measure(n) {
let view: UIView = UIView()
let id: Int = obid(view)
}
measure(n) {
let view: UIView = UIView()
let id: String = obid(view)
}
measure(n) {
let view: UIView! = UIView()
let id: Int = obid(view)
}
measure(n) {
let view: UIView! = UIView()
let id: String = obid(view)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment