# NOT RUN {
N <- 1e5
# N <- 1e8 ## too slow for CRAN
# Two examples, from slowest to fastest,
# run with N = 1e8 elements
# seconds
x <- rep_len(runif(1e4, 0, 6), N)
bench_system_time(x > 5)
bench_system_time(which(x > 5)) # 0.8
bench_system_time(which.max(x > 5)) # 0.3
bench_system_time(which_first(x > 5)) # 0.000
## Worst case: have to check all N elements
x <- double(N)
bench_system_time(x > 0)
bench_system_time(which(x > 0)) # 1.0
bench_system_time(which.max(x > 0)) # 0.4 but returns 1, not 0
bench_system_time(which_first(x > 0)) # 0.1
x <- as.character(x)
# bench_system_time(which(x == 5)) # 2.2
bench_system_time(which.max(x == 5)) # 1.6
bench_system_time(which_first(x == 5)) # 1.3
# }
Run the code above in your browser using DataLab