[Rd] speedup approxfun (code suggestion)

Thomas Petzoldt Thomas.Petzoldt at TU-Dresden.de
Thu Sep 24 23:57:13 CEST 2009


Dear R developers,

this e-mail concerns a code suggestion to slightly change approxfun, so 
that it is more efficient when called several times.

We are using approxfun (not approx) to update external variables 
(time-series) to current time points while integrating our ODE models.

It is not uncommon for these models to take in the order of 10^4 time 
steps, and sometimes we run a model > 1000 times, so approxfun is called 
many-many times. For such applications approxfun is a serious 
performance bottleneck, so we first tried to develop own functions with 
less overhead.

Then, one of us (Karline Soetart) noticed, that in the C-code of 
R_approx, each time approxfun is called, it checks the input data for 
NAs in the x and y, as well as the validity of the method and f.

For common data sets this takes about 40% of the computation time.

While testing is of course necessary for "approx", we think that for 
"approxfun", testing could be done only once, before the function is 
created.

Testing validity of the input only once, makes approxfun about 40-45% 
faster, e.g. for:

x <- seq(0, 10000)
y <- x*2

F1<- approxfun(x, y)

system.time(
  for ( i in 1:10000)
   F1(i)
)

5.50 sec for the original approxfun
2.97 sec for the patched version


This is of course just a suggestion, but we think that a patch for 
package "stats" may be of general interest and therefore more 
appropriate than providing a modified approxfun in our own package.

The code suggestion was tested with R-devel rev. 49803 and several 
contributed packages and we found no negative side effects. Please find 
the suggested patch below and please excuse if we overlooked something.

Thanks for your consideration

Best wishes,

Karline Soetaert & Thomas Petzoldt


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: stats_approx.patch
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20090924/1a50fe93/attachment.pl>


More information about the R-devel mailing list