Performance of the bit package


A performance example

Before we measure performance of the main functionality of the package, note that something simple as ‘(a:b)[-i]’ can and has been accelerated in this package:

a <- 1L
b <- 1e7L
i <- sample(a:b, 1e3)
x <- c(
  R = median(microbenchmark((a:b)[-i], times=times)$time),
  bit = median(microbenchmark(bit_rangediff(c(a, b), i), times=times)$time),
  merge = median(microbenchmark(merge_rangediff(c(a, b), bit_sort(i)), times=times)$time)
)
knitr::kable(as.data.frame(as.list(x / x["R"] * 100)), caption="% of time relative to R", digits=1)
% of time relative to R
R bit merge
100 29.9 30

The vignette is compiled with the following performance settings: 5 replications with domain size small 1000 and big 10^{6}, sample size small 1000 and big 10^{6}.

Boolean data types

“A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away.” “Il semble que la perfection soit atteinte non quand il n’y a plus rien à ajouter, mais quand il n’y a plus rien à retrancher” (Antoine de St. Exupery, Terre des Hommes (Gallimard, 1939), p. 60.)

We compare memory consumption (n=1e+06) and runtime (median of 5 replications) of the different booltypes for the following filter scenarios:

knitr::kable(
  data.frame(
    coin="random 50%",
    often="random 99%",
    rare="random 1%",
    chunk="contiguous chunk of 5%"
  ),
  caption="selection characteristic"
)
selection characteristic
coin often rare chunk
random 50% random 99% random 1% contiguous chunk of 5%
# nolint end: strings_as_factors_linter.

There are substantial savings in skewed filter situations:

% size and execution time for bit (b) and bitwhich (w) relative to logical (R) in the 'rare' scenario

% size and execution time for bit (b) and bitwhich (w) relative to logical (R) in the ‘rare’ scenario

% size and execution time for bit (b) and bitwhich (w) relative to logical (R) in the 'often' scenario

% size and execution time for bit (b) and bitwhich (w) relative to logical (R) in the ‘often’ scenario

Even in non-skewed situations the new booltypes are competitive:

% size and execution time for bit (b) and bitwhich (w) relative to logical (R) in the 'coin' scenario

% size and execution time for bit (b) and bitwhich (w) relative to logical (R) in the ‘coin’ scenario

Detailed tables follow.

% memory consumption of filter

% bytes of logical
coin often rare chunk
logical 100.0 100.0 100.0 100.0
bit 3.2 3.2 3.2 3.2
bitwhich 50.0 1.0 1.0 5.0
which 50.0 99.0 1.0 5.0
ri NA NA NA 0.0

% time extracting

% time of logical
coin often rare chunk
logical 6.4 4.1 6.2 NA
bit 13.6 12.9 13.3 NA
bitwhich 215.7 51.3 3.9 NA
which NA NA NA NA
ri NA NA NA NA

% time assigning

% time of logical
coin often rare chunk
logical 36.9 37.0 36.8 NA
bit 80.1 12.0 10.5 NA
bitwhich 184.0 43.3 40.8 NA
which NA NA NA NA
ri NA NA NA NA

% time subscripting with ‘which’

% time of logical
coin often rare chunk
logical 34.3 34.5 0.4 NA
bit 20.9 42.7 0.7 NA
bitwhich 32.8 60.1 1.4 NA
which NA NA NA NA
ri NA NA NA NA

% time assigning with ‘which’

% time of logical
coin often rare chunk
logical 20.1 39.0 0.4 NA
bit 20.0 29.6 0.5 NA
bitwhich 151.9 28.4 1.8 NA
which NA NA NA NA
ri NA NA NA NA

% time Boolean NOT

% time for Boolean NOT
coin often rare chunk
logical 16.7 16.7 16.8 16.8
bit 0.6 0.6 0.6 0.6
bitwhich 7.3 0.7 0.8 1.4
which NA NA NA NA
ri NA NA NA NA

% time Boolean AND

% time for Boolean &
coin often rare chunk
logical 97.5 29.0 21.1 20.0
bit 3.7 3.7 3.8 4.2
bitwhich 29.2 4.2 3.7 5.3
which NA NA NA NA
ri NA NA NA NA

% time Boolean OR

% time for Boolean |
coin often rare chunk
logical 100.0 27.2 25.9 24.8
bit 3.7 3.7 4.0 3.8
bitwhich 17.1 3.7 4.2 6.5
which NA NA NA NA
ri NA NA NA NA

% time Boolean EQUALITY

% time for Boolean ==
coin often rare chunk
logical 26.4 73.2 26.5 26.4
bit 4.0 3.7 3.8 3.7
bitwhich 13.8 3.5 3.7 4.8
which NA NA NA NA
ri NA NA NA NA

% time Boolean XOR

% time for Boolean !=
coin often rare chunk
logical 26.7 25.3 25.4 25.4
bit 3.6 3.6 3.7 3.9
bitwhich 14.5 3.5 3.8 4.4
which NA NA NA NA
ri NA NA NA NA

% time Boolean SUMMARY

% time for Boolean summary
coin often
logical 87.4 15.9
bit 3.5 3.4

Fast methods for integer set operations

“The space-efficient structure of bitmaps dramatically reduced the run time of sorting” (Jon Bently, Programming Pearls, Cracking the oyster, p. 7)

Execution time for R (R) and bit (b)

Execution time for R (R) and bit (b)

Execution time for R, bit and merge relative to most expensive R in 'unsorted bigbig' scenario

Execution time for R, bit and merge relative to most expensive R in ‘unsorted bigbig’ scenario

Execution time for R, bit and merge in 'sorted bigbig' scenario

Execution time for R, bit and merge in ‘sorted bigbig’ scenario

% time for sorting

sorted data relative to R’s sort
small big
sort 175.4 191.9
sortunique 82.7 34.5
unsorted data relative to R’s sort
small big
sort 27.2 35.9
sortunique 36.1 15.4

% time for unique

sorted data relative to R
small big
bit 125.9 12.5
merge 24.6 5.1
sort 0.0 0.0
unsorted data relative to R
small big
bit 110.6 11.1
merge 121.3 40.3
sort 97.8 35.1

% time for duplicated

sorted data relative to R
small big
bit 190.7 14.6
merge 30.7 5.9
sort 0.0 0.0
unsorted data relative to R
small big
bit 178.6 9.0
merge 138.1 34.8
sort 108.2 30.8

% time for anyDuplicated

sorted data relative to R
small big
bit 134.8 13.8
merge 20.5 1.6
sort 0.0 0.0
unsorted data relative to R
small big
bit 128.9 11.7
merge 142.5 43.0
sort 122.3 41.4

% time for sumDuplicated

sorted data relative to R
small big
bit 107.6 12.7
merge 21.4 5.7
sort 0.0 0.0
unsorted data relative to R
small big
bit 99.2 9.5
merge 113.7 37.9
sort 93.4 33.1

% time for match

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit NA NA NA NA
merge 83.8 0 9.9 2.7
sort 0.0 0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit NA NA NA NA
merge 261.9 68.1 72.9 42.1
sort 181.7 68.1 70.3 39.9

% time for in

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 157.7 3.3 21.4 10.0
merge 24.9 0.0 6.9 2.6
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 193.5 5.8 7.3 7.0
merge 209.1 74.0 66.1 38.5
sort 181.1 74.0 63.8 36.5

% time for notin

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 225.8 3.9 11.7 10.3
merge 22.2 0.0 3.7 2.8
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 207.0 5.7 5.8 6.1
merge 166.6 71.2 51.1 32.8
sort 144.2 71.2 49.2 31.0

% time for union

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 33.6 14.1 10.3 8.3
merge 21.0 5.5 4.1 6.8
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 34.4 11.4 8.4 6.9
merge 60.7 32.7 23.5 20.9
sort 39.4 27.8 19.9 16.3

% time for intersect

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 29.7 3.5 4.8 4.1
merge 8.9 0.0 0.0 2.4
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 32.5 4.4 3.9 3.5
merge 48.4 29.7 26.4 18.7
sort 39.5 29.7 26.4 16.3

% time for setdiff

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 24.9 1.7 15.7 5.1
merge 8.5 0.0 5.5 3.2
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 25.5 1.8 12.0 3.8
merge 50.6 23.9 30.3 22.3
sort 42.3 23.9 25.8 19.7

% time for symdiff

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 21.1 7.1 6.1 5.0
merge 4.8 1.9 1.6 1.7
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 18.6 5.8 5.3 3.8
merge 21.9 11.5 10.2 10.9
sort 17.4 9.8 8.7 9.5

% time for setequal

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 55.4 53.1 5.8 6.8
merge 13.9 16.0 3.1 3.6
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 60.8 53.3 6.7 5.9
merge 71.4 25279.4 17.5 27.6
sort 57.5 25263.0 13.6 24.1

% time for setearly

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 56.6 3.1 18.8 7.2
merge 14.1 0.0 0.1 3.8
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 59.2 1.9 5.2 5.8
merge 71.6 26.1 70.4 27.0
sort 57.7 26.1 70.3 23.5