Skip to content

Commit ac4584a

Browse files
less inline, more attributes
1 parent 06167e4 commit ac4584a

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

inst/examples/Misc/fibonacci.r

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,18 @@
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
1310
require(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( '
1914
int 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)
3623
fibR <- function(seq) {

0 commit comments

Comments
 (0)