Created
January 31, 2022 15:04
-
-
Save AlbanSagouis/a25ee83107fc6afe97545cdbce59509c to your computer and use it in GitHub Desktop.
microbenchmarking data.table::uniqueN(x) vs. length(unique(x))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(microbenchmark) | |
nrows <- 10^5 | |
tst <- data.table::data.table( | |
char = c("bb", "ds", "ok", "pb")[sample(1:4, nrows, replace = TRUE)] | |
) | |
microbenchmark(times = 1000L, | |
"base" = tst[, length(unique(char))], | |
"data.table" = tst[, data.table::uniqueN(char)] | |
) | |
# data.table alternative faster (+-2) only above 10^6 rows |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unit: milliseconds