Skip to content

Instantly share code, notes, and snippets.

@hamishknight
Created September 30, 2016 10:30
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 hamishknight/c8198d2e0fa27b624d56f1d29f380801 to your computer and use it in GitHub Desktop.
Save hamishknight/c8198d2e0fa27b624d56f1d29f380801 to your computer and use it in GitHub Desktop.
import Foundation
import QuartzCore
func benchmark(repeatCount: Int = 1, name: String = "closure", closure: () -> Void) {
let d = CACurrentMediaTime()
for _ in 0..<repeatCount {
closure()
}
let d1 = CACurrentMediaTime()-d
print("Benchmark of \(name) took \(d1) seconds")
}
class SuperClass {}
class A : SuperClass {}
class B : SuperClass {}
var a = [A]()
var b = [B]()
for i in 0..<10000000 {
a.append(A())
b.append(B())
}
benchmark {
let f = a as [SuperClass]
print(f.last!) // to prevent the compiler from optimising everything away
}
benchmark {
let f = a.map{$0 as SuperClass}
print(f.last!) // to prevent the compiler from optimising everything away
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment