File tree Expand file tree Collapse file tree 1 file changed +5
-18
lines changed
Expand file tree Collapse file tree 1 file changed +5
-18
lines changed Original file line number Diff line number Diff line change 66# # all expensive in C++ (my machine sees a 700-fold speed increase) and c) the byte
77# # compiler in R does not help here.
88
9- # # inline to compile, load and link the C++ code
10- require(inline )
11-
129# # byte compiler
1310require(compiler )
1411
15- # # we need a pure C/C++ function as the generated function
16- # # will have a random identifier at the C++ level preventing
17- # # us from direct recursive calls
18- incltxt <- '
12+ # # A C++ version compile with cppFunction
13+ fibRcpp <- cppFunction( '
1914int fibonacci(const int x) {
2015 if (x == 0) return(0);
2116 if (x == 1) return(1);
2217 return (fibonacci(x - 1)) + fibonacci(x - 2);
23- }'
24-
25- # # now use the snipped above as well as one argument conversion
26- # # in as well as out to provide Fibonacci numbers via C++
27- fibRcpp <- cxxfunction(signature(xs = " int" ),
28- plugin = " Rcpp" ,
29- incl = incltxt ,
30- body = '
31- int x = Rcpp::as<int>(xs);
32- return Rcpp::wrap( fibonacci(x) );
33- ' )
18+ }
19+ ' )
20+
3421
3522# # for comparison, the original (but repaired with 0/1 offsets)
3623fibR <- function (seq ) {
You can’t perform that action at this time.
0 commit comments