Skip to content
Prev 305879 / 398502 Next

effective way to return only the first argument of "which()"

Well, following up on this observation, which can be put under the
heading of "Sometimes vectorization can be much slower than explicit
loops" , I offer the following:

 firsti  <-function(x,k)
{
  i <- 1
  while(x[i]<=k){i <- i+1}
  i
}
user  system elapsed
   19.1     2.4    22.2
user  system elapsed
  30.45    6.75   37.46
user  system elapsed
   0.03    0.00    0.03

## About a 500 - 1000 fold speedup !
[1] 122

It doesn't seem to scale too badly, either (whatever THAT means!):
(Of course, the which() versions are essentially identical in timing,
and so are omitted)
user  system elapsed
   2.70    0.00    2.72
[1] 18200

Of course, at some point, the explicit looping is awful -- with k =
.999999, the index was about 360000, and the timing test took 54
seconds.

So I guess the point is -- as always -- that the optimal approach
depends on the nature of the data. Prudence and robustness clearly
demands the vectorized which() approaches if you have no information.
But if you do know something about the data, then you can often write
much faster tailored solutions. Which is hardly revelatory, of course.

Cheers to all,
Bert
On Wed, Sep 19, 2012 at 8:55 AM, Milan Bouchet-Valat <nalimilan at club.fr> wrote: