Dirk Eddelbuettel — written Dec 23, 2012 — source
A StackOverflow post once put the question directly in its title: Why is my recursive function so slow in R?
To cut a long story short, function calls are among the less performing parts of the R language. Operating on objects and the language itself gives us very powerful features, but the required state and stack checking for function calls is one of the prices we pay.
And as the Fibonacci sequence has such a simple definition, the simple R program can be translated easily giving us a nice example for the power of C++ particularly for function evaluations.
All that said, real computer scientists do of course insist that one should not call the sequence recursively. See for example the this post; memoization approaches are easy in R too.
Let us start with the R function:
This translates almost literally in C++:
We can time this easily thanks to the rbenchmark package:
test replications elapsed relative 2 fibCpp(20) 100 0.008 1 1 fibR(20) 100 8.591 1074
This demonstrates a rather tangible speed gain illustrating that function calls can indeed be expensive. As for the Fibonacci sequence, non-recursive approaches can of course be used to provide a speedier implementation in either R or C++.Tweet