[R] R vs Fortran

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Oct 3 00:45:29 CEST 2002


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.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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