Time the computation of Fibonacci numbers
Details
The function being timed is the following:
int fib(int n) { return ((n <= 1) ? n : fib(n - 1) + fib(n - 2)); }
Runtime for computations less than n = 15
is nearly unmeasurable.
Examples
# \donttest{
fibonacci(n = rep(20:25, 10))
#> [1] 6765 10946 17711 28657 46368 75025 6765 10946 17711 28657 46368 75025
#> [13] 6765 10946 17711 28657 46368 75025 6765 10946 17711 28657 46368 75025
#> [25] 6765 10946 17711 28657 46368 75025 6765 10946 17711 28657 46368 75025
#> [37] 6765 10946 17711 28657 46368 75025 6765 10946 17711 28657 46368 75025
#> [49] 6765 10946 17711 28657 46368 75025 6765 10946 17711 28657 46368 75025
# this function creates a global environment variable "times"
times
#> Microseconds SD Min Max Count
#> fib_20 14.160 0.880 13.837 16.661 10
#> fib_21 18.975 0.467 18.775 20.298 10
#> fib_22 36.244 0.828 35.857 38.482 10
#> fib_23 49.628 0.282 49.473 50.405 10
#> fib_24 90.124 0.229 89.889 90.711 10
#> fib_25 131.160 4.948 128.001 141.817 10
#> fib_body 3410.840 0.000 3410.840 3410.840 1
# }