[Rcpp-commits] r3148 - in pkg/Rcpp: . debian inst/examples inst/examples/Fibonacci

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jul 24 21:08:52 CEST 2011


Author: edd
Date: 2011-07-24 21:08:52 +0200 (Sun, 24 Jul 2011)
New Revision: 3148

Added:
   pkg/Rcpp/inst/examples/Fibonacci/
   pkg/Rcpp/inst/examples/Fibonacci/fib.r
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/debian/changelog
Log:
new example computing a Fibonacci number recursively in R and via Rcpp


Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2011-07-23 00:03:17 UTC (rev 3147)
+++ pkg/Rcpp/ChangeLog	2011-07-24 19:08:52 UTC (rev 3148)
@@ -1,3 +1,15 @@
+2011-07-xx  Dirk Eddelbuettel  <edd at debian.org>
+
+        * DESCRIPTION: Release 0.9.6
+        * inst/NEWS: Release 0.9.6
+        * inst/include/Rcpp/config.h: Release 0.9.6
+        * debian/*: Similarly updated for new release to Debian
+
+2011-07-24  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/examples/Fibonacci/fib.r: New example showing how to compute a
+	Fibonacci sequence using Rcpp and compare it to R and byte-compiled R
+
 2011-07-14  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/examples/RcppGibbs/RcppGibbs.R: New example RcppGibbs,
@@ -5,7 +17,7 @@
 	comparison of MCMC Gibbs Sampler implementations;
 	* inst/examples/RcppGibbs/timeRNGs.R: added short timing on Normal
 	and Gaussian RNG draws between Rcpp and GSL as R's rgamma() is seen
-	to significantly slower
+	to be significantly slower
 
 2011-07-13  Romain Francois  <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/debian/changelog
===================================================================
--- pkg/Rcpp/debian/changelog	2011-07-23 00:03:17 UTC (rev 3147)
+++ pkg/Rcpp/debian/changelog	2011-07-24 19:08:52 UTC (rev 3148)
@@ -1,3 +1,9 @@
+rcpp (0.9.6-1) unstable; urgency=low
+
+  * New release
+
+ -- Dirk Eddelbuettel <edd at debian.org>  Sat, 23 Jul 2011 09:13:48 -0500
+
 rcpp (0.9.5-1) unstable; urgency=low
 
   * New release

Added: pkg/Rcpp/inst/examples/Fibonacci/fib.r
===================================================================
--- pkg/Rcpp/inst/examples/Fibonacci/fib.r	                        (rev 0)
+++ pkg/Rcpp/inst/examples/Fibonacci/fib.r	2011-07-24 19:08:52 UTC (rev 3148)
@@ -0,0 +1,54 @@
+#!/usr/bin/r
+
+## this short example was provided in response to this StackOverflow questions:
+## http://stackoverflow.com/questions/6807068/why-is-my-recursive-function-so-slow-in-r
+## and illustrates that recursive function calls are a) really expensive in R and b) not
+## all expensive in C++ (my machine sees a 700-fold speed increase) and c) the byte
+## compiler in R does not help here.
+
+## inline to compile, load and link the C++ code
+require(inline)
+
+## we need a pure C/C++ function as the generated function
+## will have a random identifier at the C++ level preventing
+## us from direct recursive calls
+incltxt <- '
+int fibonacci(const int x) {
+   if (x == 0) return(0);
+   if (x == 1) return(1);
+   return (fibonacci(x - 1)) + fibonacci(x - 2);
+}'
+
+## now use the snipped above as well as one argument conversion
+## in as well as out to provide Fibonacci numbers via C++
+fibRcpp <- cxxfunction(signature(xs="int"),
+                   plugin="Rcpp",
+                   incl=incltxt,
+                   body='
+   int x = Rcpp::as<int>(xs);
+   return Rcpp::wrap( fibonacci(x) );
+')
+
+## for comparison, the original (but repaired with 0/1 offsets)
+fibR <- function(seq) {
+    if (seq == 0) return(0);
+    if (seq == 1) return(1);
+    return (fibR(seq - 1) + fibR(seq - 2));
+}
+
+## also use byte-compiled R function
+fibRC <- cmpfun(fibR)
+
+## load rbenchmark to compare
+library(rbenchmark)
+
+N <- 35     ## same parameter as original post
+res <- benchmark(fibR(N),
+                 fibRC(N),
+                 fibRcpp(N),
+                 columns=c("test", "replications", "elapsed",
+                           "relative", "user.self", "sys.self"),
+                 order="relative",
+                 replications=1)
+print(res)  ## show result
+


Property changes on: pkg/Rcpp/inst/examples/Fibonacci/fib.r
___________________________________________________________________
Added: svn:executable
   + *



More information about the Rcpp-commits mailing list