on an Intel Mac, OS 13.5.2
Edit: retest with purely logical input
x <- as.logical(c(NA,NA,NA,rep(1,times=20), rep(NA,times=5),rep(1,times=biglen)))
Rgames> microbenchmark(which_non_na(x),nonNA(x))
Unit: nanoseconds
expr min lq mean median uq max neval
which_non_na(x) 691 735.0 1192.88 898.0 1342.5 16088 100
nonNA(x) 45624 46285.5 47195.47 46569.5 46928.5 102274 100
Dunno how c++ would fare if it ran on numeric inputs but didn't declare "logical.
nonNA <- function(x) {
for(jn in 1:length(x)) {
if(!is.na(x[jn])) return( jn)
}
return('not found')
}
biglen=1e6
x <- c(NA,NA,NA,rep(1,times=20), rep(NA,times=5),rep(1,times=biglen))
microbenchmark(which_non_na(x),nonNA(x))
Unit: microseconds
expr min lq mean median uq max neval
which_non_na(x) 1432.809 1596.131 2398.95178 1606.1205 1615.342 11521.484 100
nonNA(x) 46.436 47.125 55.06929 47.8625 56.734 111.771 100
biglen=1e8
x <- c(NA,NA,NA,rep(1,times=20), rep(NA,times=5),rep(1,times=biglen))
microbenchmark(which_non_na(x),nonNA(x))
Unit: microseconds
expr min lq mean median uq max neval
which_non_na(x) 322243.196 327702.4310 349913.34584 353146.7080 370955.373 385839.443 100
nonNA(x) 46.642 48.3305 86.83075 100.3525 115.982 256.817 100