Skip to content
Prev 256930 / 398506 Next

Find number of elements less than some number: Elegant/fastsolution needed

Bill's code is insanely fast!

f2 <- function(x, y) length(y) - findInterval(-x, rev(-sort(y)))
 
 n1 <- 1e07
n2 <- 10^c(1,2,3,4,5,6,7)
tt <- rep(NA, 7)
x <- rnorm(n1)
for (i in 1:length(n2)){
y <- runif(n2[i])
tt[i] <- system.time(a1 <- f2(x, y))[3]
}
[1]  0.70  0.86  1.03  1.28  1.54  4.99 12.07
I would be surprised if this can be beaten even in C/C++.

Ravi.