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.2 28.5

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.3 4.8 5.0 NA
bit 11.1 11.0 11.0 NA
bitwhich 191.4 41.4 3.9 NA
which NA NA NA NA
ri NA NA NA NA

% time assigning

% time of logical
coin often rare chunk
logical 30.9 30.6 30.7 NA
bit 82.5 8.6 8.9 NA
bitwhich 187.2 31.8 25.8 NA
which NA NA NA NA
ri NA NA NA NA

% time subscripting with ‘which’

% time of logical
coin often rare chunk
logical 33.5 34.9 0.4 NA
bit 21.1 42.7 0.6 NA
bitwhich 33.5 62.5 1.2 NA
which NA NA NA NA
ri NA NA NA NA

% time assigning with ‘which’

% time of logical
coin often rare chunk
logical 20.6 40.5 0.6 NA
bit 17.7 25.7 0.4 NA
bitwhich 149.8 15.5 1.4 NA
which NA NA NA NA
ri NA NA NA NA

% time Boolean NOT

% time for Boolean NOT
coin often rare chunk
logical 11.4 11.6 11.9 11.4
bit 0.4 0.4 0.4 0.4
bitwhich 7.0 0.6 0.5 1.1
which NA NA NA NA
ri NA NA NA NA

% time Boolean AND

% time for Boolean &
coin often rare chunk
logical 100.0 25.6 18.6 48.0
bit 2.7 3.3 2.9 3.0
bitwhich 22.4 3.7 3.2 3.9
which NA NA NA NA
ri NA NA NA NA

% time Boolean OR

% time for Boolean |
coin often rare chunk
logical 95.3 19.2 20.3 18.6
bit 3.2 2.9 3.0 2.9
bitwhich 12.8 3.2 3.7 4.8
which NA NA NA NA
ri NA NA NA NA

% time Boolean EQUALITY

% time for Boolean ==
coin often rare chunk
logical 17.7 18.2 18.6 18.8
bit 2.9 2.8 2.8 3.1
bitwhich 8.9 3.2 2.9 3.3
which NA NA NA NA
ri NA NA NA NA

% time Boolean XOR

% time for Boolean !=
coin often rare chunk
logical 18.8 18.8 18.3 18.6
bit 3.0 2.9 2.8 3.1
bitwhich 9.0 3.0 3.0 3.2
which NA NA NA NA
ri NA NA NA NA

% time Boolean SUMMARY

% time for Boolean summary
coin often
logical 85.8 12.5
bit 3.7 3.7

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 189.5 294.1
sortunique 70.6 35.0
unsorted data relative to R’s sort
small big
sort 25.2 38.6
sortunique 15.7 15.2

% time for unique

sorted data relative to R
small big
bit 111.8 13.9
merge 26.4 3.0
sort 0.0 0.0
unsorted data relative to R
small big
bit 131.3 24.3
merge 122.7 45.3
sort 93.2 42.4

% time for duplicated

sorted data relative to R
small big
bit 221.9 12.7
merge 30.8 3.7
sort 0.0 0.0
unsorted data relative to R
small big
bit 223.9 13.2
merge 138.9 51.5
sort 106.9 47.1

% time for anyDuplicated

sorted data relative to R
small big
bit 150.1 9.8
merge 20.5 1.1
sort 0.0 0.0
unsorted data relative to R
small big
bit 128.1 14.1
merge 132.2 53.4
sort 111.1 51.6

% time for sumDuplicated

sorted data relative to R
small big
bit 124.1 15.9
merge 26.7 5.5
sort 0.0 0.0
unsorted data relative to R
small big
bit 101.6 10.8
merge 102.8 43.1
sort 80.1 39.2

% time for match

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit NA NA NA NA
merge 31.7 0 10.1 3.2
sort 0.0 0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit NA NA NA NA
merge 214.0 70.8 78.0 59.7
sort 182.5 70.8 75.5 56.0

% time for in

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 161.2 2.3 16.0 9.7
merge 22.3 0.0 7.5 2.0
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 160.9 4.4 4.9 10.2
merge 182.0 85.7 67.5 61.5
sort 158.0 85.7 65.3 59.0

% time for notin

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 209.4 2.3 7.2 13.2
merge 23.5 0.0 3.4 3.3
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 216.5 3.4 6.7 8.1
merge 148.2 68.1 49.6 46.4
sort 125.8 68.1 48.0 44.1

% time for union

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 35.2 21.4 14.6 8.2
merge 20.0 4.7 3.3 5.6
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 33.8 9.2 18.3 10.7
merge 54.9 29.3 38.6 26.7
sort 34.1 26.1 34.2 22.1

% time for intersect

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 31.0 3.7 5.2 4.6
merge 7.6 0.0 0.0 1.3
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 28.7 4.6 4.2 4.9
merge 40.4 33.4 29.3 25.0
sort 33.1 33.4 29.3 23.3

% time for setdiff

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 28.6 1 12.7 4.6
merge 9.4 0 3.9 1.9
sort 0.0 0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 27.5 1.1 11.0 6.0
merge 47.7 22.6 32.7 35.4
sort 38.3 22.6 29.0 32.8

% time for symdiff

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 22.6 6.6 5.5 4.4
merge 3.5 1.4 1.1 0.9
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 17.8 5.3 7.1 5.8
merge 17.2 12.1 12.0 17.3
sort 14.0 10.9 10.7 16.0

% time for setequal

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 58.3 57.4 6.1 6.8
merge 10.9 10.8 1.5 1.7
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 63.9 53.1 6.6 5.7
merge 63.4 31025.4 16.7 27.0
sort 51.8 31015.0 14.7 25.2

% time for setearly

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 61.5 4 19.6 6.2
merge 12.3 0 0.1 1.6
sort 0.0 0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 58.3 3.1 6.3 6.5
merge 58.9 30.9 64.5 31.5
sort 47.5 30.9 64.4 29.5