Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Benchmark_on_iOS

to4iki
October 08, 2016

 Benchmark_on_iOS

to4iki

October 08, 2016
Tweet

More Decks by to4iki

Other Decks in Programming

Transcript

  1. func measure(f: () -> Void) { let start = CACurrentMediaTime()

    f() let end = CACurrentMediaTime() print("Time: \(end - start)") } // exc measure { aswesome() }
  2. func measure<A>(f: () -> A) -> A { defer {

    print("Time: \(end - start)") } let start = CACurrentMediaTime() let result = f() let end = CACurrentMediaTime() return result } // exc let result = measure { aswesome() }
  3. to4iki/Benchmark • Inspired by Ruby Benchmark Module • Swift3.0੡ •

    XCTest#measureʹد͍ͤͯΔ • ؆қతͳϝτϦΫεܭଌΛߦ͏
  4. let time = Benchmark.realTime(label: "awesome: ") { sleep(3) } print(time)

    // awesome: 0.030s let metrics = Benchmark.measure(label: "[awesome]") { awesome() } print(metrics) // [awesome] // average: 0.030s // STDDEV: 4%
  5. let n = 100000 Benchmark.bm(labels: ["total: ", "ave: "]) {

    (r: Benchmark.Report) -> [TimeInterval] in let ex1 = r.time(label: "for: ") { for i in 1...n { String(i) } } let ex2 = r.time(label: "forEach: ") { (1...n).forEach({ String($0) }) } let total = ex1.elapsed + ex2.elapsed let ave = total / 2 return [total, ave] } // for: 0.027s // forEach: 0.037s // total: 0.064s // ave: 0.032s