Skip to contents

Time the computation of Fibonacci numbers

Usage

fibonacci(n)

Arguments

n

vector giving integers for which to compute the Fibonacci sum

Value

vector of integers giving the Fibonacci sum for each element in n

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(10*(1:4), 10))
#>  [1]        55      6765    832040 102334155        55      6765    832040
#>  [8] 102334155        55      6765    832040 102334155        55      6765
#> [15]    832040 102334155        55      6765    832040 102334155        55
#> [22]      6765    832040 102334155        55      6765    832040 102334155
#> [29]        55      6765    832040 102334155        55      6765    832040
#> [36] 102334155        55      6765    832040 102334155
# this function creates a global environment variable "times"
times
#>       Name Milliseconds    SD Count
#> 1   fib_10        0.001 0.000    10
#> 2   fib_20        0.013 0.001    10
#> 3   fib_30        1.367 0.016    10
#> 4   fib_40      166.220 0.102    10
#> 5 fib_body     1676.071 0.000     1
# }