[Rd] HOW TO AVOID LOOPS
milbo at sonic.net
Mon Apr 14 22:22:51 CEST 2008
> Le sam. 12 avr. à 12:47, carlos martinez a écrit :
> Looking for a simple, effective a minimum execution time solution.
> For a vector as:
> To transform it to the following vector without using any loops:
Here is a fast solution using the Ra just-in-time compiler
if (length(x) > 1)
for (i in 2:length(x))
x[i] <- x[i-1] + 1
The times in seconds for various solutions mailed to r-devel are listed
below. There is some variation between runs and with the contents of x. The
times shown are for
set.seed(1066); x <- as.double(runif(1e6) > .5)
This was tested on a WinXP 3 GHz Pentium D with Ra 1.0.7 (based on R 2.6.2).
The code to generate these results is attached.
jit 0.25 # code is shown above, 7 secs with standard R 2.6.2>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
More information about the R-devel