Time a function

var benchmark = frequire('Benchmark')

func fac_rec(n) {
    n == 0 ? 1 : (n * __FUNC__(n - 1))
}

func fac_iter(n) {
    var prod = 1
    n.times { |i|
        prod *= i
    }
    prod
}

var result = benchmark.timethese(-3, Hash(
    'fac_rec'  => { fac_rec(20)  },
    'fac_iter' => { fac_iter(20) },
))

benchmark.cmpthese(result)

Output:

Last updated

Was this helpful?