Created
September 30, 2016 10:30
-
-
Save hamishknight/c8198d2e0fa27b624d56f1d29f380801 to your computer and use it in GitHub Desktop.
This file contains 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
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