Skip to content

Instantly share code, notes, and snippets.

@klmr
Created January 30, 2014 20:44
Show Gist options
  • Save klmr/8718294 to your computer and use it in GitHub Desktop.
Save klmr/8718294 to your computer and use it in GitHub Desktop.
Benchmarking performance of row selection via `-which(…)` vs. `!…`.
> library(microbenchmark)
> m <- data.frame(a = sample.int(10, 100000, replace=T),
b = sample.int(10, 100000, replace=T))
> microbenchmark(m[-which(m[, 1] == 4 & m[, 2] == 5), ], m[! (m[, 1] == 4 & m[, 2] == 5), ])
Unit: milliseconds
expr min lq median uq max neval
m[-which(m[, 1] == 4 & m[, 2] == 5), ] 27.59480 29.64628 30.09091 30.69976 42.04620 100
m[!(m[, 1] == 4 & m[, 2] == 5), ] 29.00876 29.70103 30.41829 33.10700 42.29671 100
> m <- as.matrix(m)
microbenchmark(m[-which(m[, 1] == 4 & m[, 2] == 5), ], m[! (m[, 1] == 4 & m[, 2] == 5), ])
Unit: milliseconds
expr min lq median uq max neval
m[-which(m[, 1] == 4 & m[, 2] == 5), ] 9.826798 10.73116 11.15770 13.74899 19.66254 100
m[!(m[, 1] == 4 & m[, 2] == 5), ] 9.290829 10.49009 11.08083 12.46361 18.83631 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment