One goal of the {svTidy} package is to provide an interface that is similar to {dplyr} and {tidyr} to performant code (both for speed and memory use), possibly using the {data.table} or {collapse} packages under the hood.
In this document, we compare speed and memory use of {svTidy} with {dplyr} and {tidyr}. Keep in mind that most benchmarks (including the present ones) are artificial and do not necessarily reflect real use cases. Test with your own data. Also, do you really need faster or more memory efficient code? It depends on your particular context! Also take your hardware into account (number of CPU and amount of RAM).
We test {svTidy} function both in standard evaluation (SE) and non standard evaluation (NSE) modes: the later one usually requires more computing time, but it is more convenient to write and read and closer to {dplyr}/{tidyr} syntax.
Preparation: using 3/4 of available cores for parallel code in {data.table} and {collapse}.
data.table::setDTthreads(percent = 75)
(.nthreads <- data.table::getDTthreads())
#> [1] 3
options(collapse_nthreads = .nthreads)
options(collapse_na.rm = FALSE)
options(collapse_mask = "all")Small and medium data sets.
# Small one
data(mtcars)
mtcars <- as_tibble(mtcars, rownames = "model")
mtcars_dt <- as.data.table(mtcars)
# Medium one
data(babynames, package = 'babynames')
babynames <- as_tibble(babynames)
babynames_dt <- as.data.table(babynames)Here is a couple of examples of fast and memory efficient {svTidy} functions.
filter_()Small data set.
# Note: collapse::qDF() = quickly convert to a data.frame, for identical results
bench::mark(
dplyr = filter(mtcars, mpg > 20) |> qDF(),
data.table = mtcars_dt[mpg > 20] |> qDF(),
svTidyNSE = filter_(mtcars, ~mpg > 20) |> qDF(),
svTidySE = filter_(mtcars, mtcars$mpg > 20) |> qDF())
#> # A tibble: 4 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 dplyr 670.2µs 710.4µs 1344. 1.36MB 8.51
#> 2 data.table 104.1µs 112.1µs 8563. 1.82MB 8.54
#> 3 svTidyNSE 52.8µs 56.7µs 17038. 154.43KB 8.56
#> 4 svTidySE 44.8µs 48.4µs 20054. 43.45KB 10.8Medium data set.
bench::mark(
dplyr = filter(babynames, n > 1000) |> qDF(),
data.table = babynames_dt[n > 1000] |> qDF(),
svTidyNSE = filter_(babynames, ~n > 1000) |> qDF(),
svTidySE = filter_(babynames, babynames$n > 1000) |> qDF())
#> # A tibble: 4 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 dplyr 10.72ms 11.09ms 76.2 31.4MB 38.1
#> 2 data.table 4.78ms 5.03ms 157. 16.7MB 40.0
#> 3 svTidyNSE 4.32ms 4.44ms 225. 16.7MB 55.4
#> 4 svTidySE 4.3ms 4.47ms 186. 16.7MB 33.8arrange_()bench::mark(
dplyr = arrange(mtcars, cyl, desc(vs)) |> qDF(),
data.table = mtcars_dt[order(cyl, -vs)] |> qDF(),
svTidyNSE = arrange_(mtcars, ~cyl, ~ -vs) |> qDF(),
svTidySE = arrange_(mtcars, 'cyl', '-vs') |> qDF())
#> # A tibble: 4 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 dplyr 2.24ms 2.29ms 433. 591.4KB 8.65
#> 2 data.table 155.52µs 162.34µs 5987. 38.9KB 8.58
#> 3 svTidyNSE 99.9µs 105.09µs 9227. 92.9KB 10.9
#> 4 svTidySE 60.61µs 64.41µs 15032. 85.1KB 10.8bench::mark(
dplyr = arrange(babynames, sex, desc(n)) |> qDF(),
data.table = babynames_dt[order(sex, -n)] |> qDF(),
svTidyNSE = arrange_(babynames, ~sex, ~ -n) |> qDF(),
svTidySE = arrange_(babynames, 'sex', '-n') |> qDF())
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 4 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 dplyr 70.7ms 89.8ms 11.6 112.7MB 11.6
#> 2 data.table 60.2ms 68.1ms 11.0 73.4MB 6.30
#> 3 svTidyNSE 26.8ms 30.5ms 26.9 73.4MB 7.69
#> 4 svTidySE 26.2ms 26.8ms 29.9 73.4MB 9.96Not all {svTidy} functions are currently faster or more memory efficient than their {dplyr} or {tidyr} counterparts. Those still need refactoring to be optimized. Here are some examples.
bind_rows_()df1 <- tibble(x = 1:2, y = letters[1:2])
df1_dt <- as.data.table(df1)
bench::mark(
dplyr = bind_rows(df1, df1) |> qDF(),
base = rbind(df1, df1) |> qDF(),
data.table = rbindlist(list(df1_dt, df1_dt)) |> qDF(),
svTidy = bind_rows_(df1, df1) |> qDF(),
svTidy2 = bind_rows_(list(df1, df1)) |> qDF())
#> # A tibble: 5 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 dplyr 89.8µs 95.4µs 10119. 96.4KB 8.97
#> 2 base 79.4µs 83.7µs 11610. 47.5KB 10.8
#> 3 data.table 114.7µs 120.2µs 8063. 121.5KB 6.30
#> 4 svTidy 622.9µs 657µs 1504. 203.9KB 8.58
#> 5 svTidy2 624.1µs 655.2µs 1515. 52.5KB 8.58bench::mark(
dplyr = bind_rows(babynames, babynames) |> qDF(),
base = rbind(babynames, babynames) |> qDF(),
data.table = rbindlist(list(babynames_dt, babynames_dt)) |> qDF(),
svTidy = bind_rows_(babynames, babynames) |> qDF(),
svTidy2 = bind_rows_(list(babynames, babynames)) |> qDF())
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 5 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 dplyr 75.99ms 87.62ms 5.90 132MB 1.97
#> 2 base 190.65ms 196.35ms 4.81 256MB 1.60
#> 3 data.table 59.98ms 90.48ms 11.0 132MB 3.66
#> 4 svTidy 1.68s 1.68s 0.595 368MB 1.19
#> 5 svTidy2 1.55s 1.55s 0.644 338MB 0.644bind_cols_()df1 <- tibble(x = 1:2, y = letters[1:2])
df2 <- tibble(z = 10:11, w = factor(5:6))
df1_dt <- as.data.table(df1)
df2_dt <- as.data.table(df2)
bench::mark(check = FALSE,
dplyr = bind_cols(df1, df2) |> qDF(),
base = cbind(df1, df2) |> qDF(),
data.table = cbind(df1_dt, df2_dt) |> qDF(),
svTidy = bind_cols_(df1, df2) |> qDF())
#> # A tibble: 4 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 dplyr 145µs 156µs 5963. 32.87KB 2.10
#> 2 base 195µs 203µs 4807. 2.19KB 2.09
#> 3 data.table 164µs 171µs 5691. 187.33KB 2.09
#> 4 svTidy 152µs 159µs 6087. 13.88KB 2.09