3 ## New and shorter version of Fibonacci example using Rcpp 0.9.16 or later features
4 ## The the sibbling file 'fibonacci.r' for context
6 require(Rcpp) # no longer need inline
9 fibR <- function(seq) {
10 if (seq < 2) return(seq)
11 return (fibR(seq - 1) + fibR(seq - 2))
16 int fibonacci(const int x) {
18 return (fibonacci(x - 1)) + fibonacci(x - 2);
22 fibCpp <- cppFunction(cpptxt) # compiles, load, links, ...
24 ## load rbenchmark to compare
27 N <- 35 ## same parameter as original post
28 res <- benchmark(fibR(N), fibCpp(N),
29 columns=c("test", "replications", "elapsed", "relative"),
30 order="relative", replications=1)
31 print(res) ## show result