Other benchmarks

 

Introduction

This page benchmarks some of the functions from ‘broadcast’ with some near-equivalent functions from other packages. The code is given here also.

 

abind::abind()

In this section, te performance of the bind_array() function from ‘broadcast’ is compared to the performance of the abind() function from the ‘abind’ package.

 

The following code was used:

n <- 110L
nms <- function(n) sample(letters, n, TRUE)
x <- array(as.double(1:25), c(n, n, n))
y <- array(as.double(-1:-25), c(n, n, n))
dimnames(x) <- lapply(dim(x), nms)
dimnames(y) <- lapply(dim(y), nms)
input <- list(x, y, x)

gc()
bm_abind <- bench::mark(
  abind = abind::abind(input, along = 2),
  broadcast = bind_array(input, 2),
  min_iterations = 100,
  check = FALSE # because abind adds empty dimnames
)
summary(bm_abind)
plot(bm_abind)

And here are the results:

#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 abind        34.7ms   41.3ms      23.8   121.9MB    0.736
#> 2 broadcast    14.2ms   14.9ms      63.2    60.9MB    1.29
#> Loading required namespace: tidyr

Clearly, the bind_array() function from ‘broadcast’ is about 2 to 3 times faster than the abind() function from the ‘abind’ package. It is also about 2 times more memory efficient.

 

Rfast::Outer()

An outer computation is a special case of broadcasting, namely a broadcasting computation between a row-vector and a column-vector. The outer() function from base ‘R’ is too slow and consumes too much memory to provide any meaningful benchmark. But the ‘Rfast’ package provides a very fast implementation of the outer() function. It may be interesting how broadcasted operations hold up to the famously fast ‘Rfast’ package.

Here the outer-sum between a row-vector x and column-vector y (both have 9000 elements) is computed using Rfast::outer() and broadcast::bc.num(), and their speeds and memory consumption are compared.

The following code was used:


n <- 9e3
x <- array(rnorm(10), c(1, n))
y <- array(rnorm(10), c(n, 1))
gc()
bm_outer <- bench::mark(
  Rfast = Rfast::Outer(x, y, "+"),
  broadcast = bc.num(x, y, "+"),
  min_iterations = 100
)
summary(bm_outer)
plot(bm_outer)

And here are the results:

#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 Rfast        98.8ms    102ms      9.14     619MB     2.29
#> 2 broadcast    98.9ms    107ms      8.72     618MB     2.18

It seems that the implementations of ‘broadcast’ and the blazingly fast ‘Rfast’ package reach similar speeds and use the same amount of memory.

Note, however, that Rfast::Outer() unfortunately only supports numeric vectors, and does not provide higher-dimensional broadcasting. ‘broadcast’, on the other hand, supports all atomic types as well as the list recursive type, and supports arrays of any dimensions up to 16 dimensions.

 

%r+% operator from ‘collapse’

The impressive ‘collapse’ package supports a large set of blazingly fast functions for a large variety of tasks. One of these is the x %r% v operator. Given a matrix x and a vector v, x %r+% v will add v to every row of x. Using this function in this way is equivalent to the bc.num() function, using a column-vector for v.

Here these 2 approaches are benchmarked.

The code used was as follows:


n <- 8e3
x <- matrix(rnorm(10), n, n)
v <- array(rnorm(10), c(1, n))
bm_collapse_row <- bench::mark(
  collapse = x %r+% v,
  broadcast = bc.num(x, v, "+"),
  min_iterations = 100
)
summary(bm_collapse_row)
plot(bm_collapse_row)

And here are the results:

#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 collapse       93ms   98.5ms      9.85     488MB     2.02
#> 2 broadcast    98.3ms    106ms      9.20     488MB     1.88

The ‘collapse’ package is slightly faster than ‘broadcast’ in this case. This does show how super fast ‘collapse’ truly is.