Forked from kristopherjohnson/executionTimeInterval.swift
Created
July 21, 2016 12:51
-
-
Save AnselmeKotchap/5849e205d4b65724f778f465db9327b4 to your computer and use it in GitHub Desktop.
Calculate execution time for a block of Swift code
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
import QuartzCore | |
func executionTimeInterval(block: () -> ()) -> CFTimeInterval { | |
let start = CACurrentMediaTime() | |
block(); | |
let end = CACurrentMediaTime() | |
return end - start | |
} | |
// DEMO (paste all this into a playground) | |
// Non-memoized Fibonacci generator | |
func fib(n: Int) -> Int { | |
assert(n >= 0) | |
switch n { | |
case 0, 1: return 1 | |
default: return fib(n-1) + fib(n-2) | |
} | |
} | |
let fib8 = executionTimeInterval { | |
let x = fib(8) | |
println("fib(8) = \(x)") | |
} | |
let fib10 = executionTimeInterval { | |
let x = fib(10) | |
println("fib(10) = \(x)") | |
} | |
let fib12 = executionTimeInterval { | |
let x = fib(12) | |
println("fib(12) = \(x)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment