views:

241

answers:

3

The classic and brilliant Programming Perl reference book has a section in which the authors provide a list of advice for how to write Perl that is maximally computationally efficient, followed by a list of advice for how to write Perl that is maximally programmer efficient, followed by more advice for maintainer efficient, porter efficient, and user efficient. The advice is usually completely contradictory. (E.g., "use globals", "don't use globals.")

I thought of this while working on turning some "programmer efficient" R code into "computationally and maintainer efficient" code.

What are some interesting and useful tips for R style along these lines? What practices are maximally programmer efficient, and what are the equivalent practices that address other notions of efficiency?

A: 

I think style guidelines (as discussed before on SO) help for programmer efficiency. R Core seems to agree by providing some hints (and Emacs parameters for consistent indenting).

Execution efficiency is more difficult to achieve by decree. You may have to fall back to rules of thumb ('vectorise') as well as profiling.

Dirk Eddelbuettel
+1  A: 

What you can count on being slow is anything that, in a loop, rebuilds data, like appending elements to a vector, if it is done a lot.

Mike Dunlavey
+10  A: 
Programmer efficient                 |   Computationally efficient
                                     |
Write everything in R                |   Call C/Fortran routines
Reuse code                           |   Custom create everything 
  (functions not scripts,            |
  packages not individual functions) |
Use high level functions             |   Use low-level functions
Write things that work               |   Write it, profile it, optimise it.
                                     |     Repeat ad infinitum.
Richie Cotton
Would love to see some more examples like this, especially with more details!
Harlan