Task

Write a program which uses a timer (with the least granularity available
on your system) to time how long a function takes to execute.

Whenever possible, use methods which measure only the processing time used
by the current process; instead of the difference in system time
between start and finish, which could include time used by
other processes on the computer.

This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.




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)