[R] R vs Fortran

Paul Gilbert pgilbert at bank-banque-canada.ca
Thu Oct 3 16:30:17 CEST 2002


Peter Dalgaard BSA wrote:
> 
> Robin Hankin <r.hankin at auckland.ac.nz> writes:
> 
> >
> >     do 10 i=1,200
> >       do 20 j=1,200
> >         flux(i,j) = ( r(i,j)*u(i+1,j) + r(i+1,j)*u(i,j) ) /2
> > 20    continue
> > 10  continue
> >
> > where r is the density and u the x-component of velocity.  I might
> > need to do this or similar-looking things such as u(i+1,j)*v(i,j+1)
> > perhaps a hundred times in my Fortran code.  But in R we could have
> >
> > flux =  ( r*right(u) + right(r)*u ) /2
> >
> > [where right <- function(x){cbind(x[,-1],NA)} ]
> >
> > To my mind, the functional form is much better: it's vectorized and
> > clear and terse.  My question is, is it fast? (or more precisely, how
> > much slower would this nice approach be than my clunky old Fortran).
> > I guess I could tolerate a factor of two or three, and wait for a
> > shiny next-generation PC.
> >
> >
> > any comments anyone?
> 
> I would be highly surprised if it turned out to be even remotely fast
> in R...
> 
> This kind of thing generally needs a compiled language like C(++) or
> Fortran. Preferably with good optimisers.

I guess speed is relative. I've had pretty good luck with similar
calculations in R. If you are repeating the calculation millions of
times then you will be in trouble, but if you are only repeating it
hundreds of times then I find the other overheads are more important
(such as user error checking).

However, you've forgotten what might be your most attractive option:
leave the computationally intensive blocks in clunky old Fortran
(separated in nice clean little function blocks) and glue it together
with R. But I would not even bother with this until you find it is too
slow.

Paul
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list