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
6require(Rcpp) # no longer need inline
10 if (seq < 2) return(seq)
11 return (fibR(seq - 1) + fibR(seq - 2))
16int fibonacci(const int x) {
18 return (fibonacci(x - 1)) + fibonacci(x - 2);
22fibCpp <- cppFunction(cpptxt) # compiles, load, links, ...
24## load rbenchmark to compare
27N <- 35 ## same parameter as original post
28res <- benchmark(fibR(N), fibCpp(N),
29 columns=c("test", "replications", "elapsed", "relative"),
30 order="relative", replications=1)
31print(res) ## show result