Timings of function execution in R [was Re: R in Industry]
On 2/9/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
The other reason why pmin/pmax are preferable to your functions is that they are fully generic. It is not easy to write C code which takes into account that <, [, [<- and is.na are all generic. That is not to say that it is not worth having faster restricted alternatives, as indeed we do with rep.int and seq.int. Anything that uses arithmetic is making strong assumptions about the inputs. It ought to be possible to write a fast C version that worked for atomic vectors (logical, integer, real and character), but is there any evidence of profiled real problems where speed is an issue?
I had an example just last month of an MCMC calculation where profiling showed that pmax(x,0) was taking about 30% of the total time. I used
function(x) {z <- x<0; x[z] <- 0; x}
which was significantly faster. I didn't try the arithmetic solution. Also, I didn't check if a solution like this would still be faster when both arguments are vectors (but there was a recent mailing list thread where someone else did).
-thomas
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle