[Rcpp-devel] How to increase the coding efficiency

Dirk Eddelbuettel edd at debian.org
Tue Dec 4 22:03:27 CET 2012


On 4 December 2012 at 20:39, Yan Zhou wrote:
| You cannot expect C++ to magically make your code faster. If speed is of
| concern, you need profiling to find the bottleneck instead of blind guessing. I

Precisely. As I told Honglang just yesterday on this very list:

   Some general advice: when something complicated does not work, I generally
   try to restart with something simpler, and make sure it works. 

So decompose, and study smaller problems.

| am not quite sure how to profile an Rcpp R program. Maybe someone else can
| help.

Same as with other compiled code: tell the compiler to insert the markers,
then analyse the results. For gcc et al a profiler is built in, external
tools such as Google Perftools (same kit that brings us tcmalloc etc) is
there too and I have examples in my slides from the 'intro to hpc with R'
talks I used to give.

| But my intuition is that your program simply need that much time. The reason
| that R is not much slower is perhaps the linear algebra operations took much of
| the time, and that is as efficient in R as in C++. If you built R with an
| optimized Blas, the difference may be even smaller.

Rcpp (and RcppArmadillo) code links to what R is linked to, so you get the same BLAS/

| The bottom line is that C++ is no magic. The reason that it can be an order of
| magnitude faster than R sometime is that the close to metal natural enable one
| to take out some middleware. The computation that requires a bulk of time still
| need the same time whatever language you use.
| 
| And even there is something to improve in coding efficiency, that is not
| something you can learn through just a few emails. This kind if thing take
| time, practice and experience.

Well put. Could not agree more. 

There is neither a free lunch, nor magic, but diligent work can get you a
long way towards you goal.

Nice case in point: Romain just these days shaving a few percentage points of
his new hashing class in Rcpp's SVN.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list