Last active
December 9, 2016 01:37
-
-
Save aleclarson/be3afc3ba677e313a570 to your computer and use it in GitHub Desktop.
Convenience functions for ObjectIdentifier hashes
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
/// 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 | |
} |
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
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