performance of dcast could be improved
@tdhock is already working on this.
Since Aug 8, 2024.
Assessment
This issue has not been assessed yet.
Description
I discovered that there is room to improve the computation time of dcast.
Here is an example:
library(data.table)
num.threads <- 1
setDTthreads(num.threads)
w.res <- atime::atime(
N=2^seq(1,50),
setup={
(row.id.vec <- 1+(seq(0,N-1) %% nrow(iris)))
N.dt <- melt(data.table(iris[row.id.vec,])[,orig.row.i:=.I],measure=1:4)
},
seconds.limit=1,
dcast=dcast(N.dt, orig.row.i ~ variable),
pivot=collapse::pivot(N.dt, how="w", ids="orig.row.i", values="value", names="variable", nthreads=num.threads))
w.refs <- atime::references_best(w.res)
w.pred <- predict(w.refs)
N.at.limit <- dcast(w.pred$prediction, . ~ expr.name, value.var="N")
library(ggplot2)
plot(w.pred)+ggtitle(
sprintf("pivot %.1fx faster than dcast", N.at.limit[, pivot/dcast]))
The code above uses melt to construct N.dt which is a table of size N *4 (where 4=number of numeric columns in iris data), a long version of iris data.
Then dcast is used to return the long iris data into their original/wide shape.
This is a simple operation which just involves allocating a new wide table and copying the values from the long table to the wide table.
On my computer I got the result below.
The figure shows that collapse::pivot is faster than dcast for all N, and about ~10x faster asymptotically.
Note that this observation (10x speed difference) only applies to the simple case of copying values into a new shape (no aggregation).
In the case with fun.aggregate, performance looks much more reasonable, see my recent blog post https://tdhock.github.io/blog/2024/collapse-reshape/ for full details.
- 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 ·