uniqueN() is very slow compared to length(unique())
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- r
- Domain
- performance
Research direction
Start by running the supplied R reprex and comparing uniqueN() with length(unique()) for character and double columns, including the one-thread case. Trace the uniqueN() entry point to investigate why grouped calls are much slower; done means the reported performance gap is addressed and the comparison is covered by regression checks.
Written by the indexing model from the issue text.
Description
I'm using the lastest dev version of data.table. Still uniqueN() is an order of magnitude slower than length(unique()) . So slow I think it should be tagged as a bug ... See the reprex example below.
Note, the below code uses 4 threads (on a Win7 computer). If we set the thread number to 1, the time cost will be reduced to a half but still significantly slower than length(unique()).
(In fact, the reason I notice this is because I have a daily routine script costs maybe 20 minutes... and trying to improve the speed leads me to the cause - uniqueN())
Character
library(data.table)
set.seed(1000)
mk_rd_words <- function(min = 4, max = 20) {
n <- floor(runif(1, min, max))
paste0(sample(c(letters, LETTERS), size = n, replace = TRUE), collapse = '')
}
words <- vapply(1:1000, function(x) mk_rd_words(4, 50), FUN.VALUE = 'a')
n <- 1e4
tbl <- data.table(
a = sample(words, size = n, replace = TRUE),
b = sample(words, size = n, replace = TRUE)
)
microbenchmark::microbenchmark(
times = 100,
tbl[, .(N = uniqueN(b)), keyby = a],
tbl[, .(N = length(unique(b))), keyby = a]
)
#> Unit: milliseconds
#> expr min lq
#> tbl[, .(N = uniqueN(b)), keyby = a] 169.13260 171.651133
#> tbl[, .(N = length(unique(b))), keyby = a] 8.12066 8.607032
#> mean median uq max neval
#> 176.649940 173.972808 181.373316 201.70846 100
#> 9.233874 8.738746 9.208014 16.28779 100
Created on 2019-08-02 by the reprex package (v0.2.1)
Double
library(data.table)
set.seed(1000)
n <- 1e4
tbl <- data.table(
a = sample(1:1e3, size = n, replace = TRUE),
b = sample(1:1e3, size = n, replace = TRUE)
)
microbenchmark::microbenchmark(
times = 100,
tbl[, .(N = uniqueN(b)), keyby = a],
tbl[, .(N = length(unique(b))), keyby = a]
)
#> Unit: milliseconds
#> expr min lq
#> tbl[, .(N = uniqueN(b)), keyby = a] 107.329319 111.531912
#> tbl[, .(N = length(unique(b))), keyby = a] 5.777745 5.980306
#> mean median uq max neval
#> 119.497038 115.412347 124.303334 158.04780 100
#> 6.992722 6.314294 7.678619 13.08759 100
Created on 2019-08-02 by the reprex package (v0.2.1)
- Dominant language
- R
- Stars
- 3.9k
- Forks
- 1.1k
- Avg merge
- 14h 4m
- Merged PRs (30d)
- 4
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·