Skip to content

Instantly share code, notes, and snippets.

@norio-nomura
Last active August 29, 2015 14:14
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 norio-nomura/399310ba93b730685888 to your computer and use it in GitHub Desktop.
Save norio-nomura/399310ba93b730685888 to your computer and use it in GitHub Desktop.
Playgroundのテンプレートにbenchmark定義を入れてる。
import Foundation
import QuartzCore
func benchmark<T>(block: () -> T) -> String {
let measure = {(begin: CFTimeInterval) -> CFTimeInterval in block(); return CACurrentMediaTime() - begin}
let result = String(format: "Total Runtime: %g sec", measure(CACurrentMediaTime()))
println(result)
return result
}
func benchmark(block: () -> ()) -> String {
let measure = {(begin: CFTimeInterval) -> CFTimeInterval in block(); return CACurrentMediaTime() - begin}
let result = String(format: "Total Runtime: %g sec", measure(CACurrentMediaTime()))
println(result)
return result
}
// MARK: -
benchmark {
// write code here
}
@norio-nomura
Copy link
Author

Swiftでクロージャの戻り値をジェネリックにしても、値を返さない(Void)にはマッチしないので、専用化が必要になる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment