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 25.5 25.3

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 20.4 6.7 6.7 NA
bit 18.3 18.4 19.0 NA
bitwhich 26.8 55.1 6.7 NA
which NA NA NA NA
ri NA NA NA NA

% time assigning

% time of logical
coin often rare chunk
logical 40.3 40.2 40.6 NA
bit 80.0 13.9 13.3 NA
bitwhich 183.0 43.5 43.3 NA
which NA NA NA NA
ri NA NA NA NA

% time subscripting with ‘which’

% time of logical
coin often rare chunk
logical 22.1 35.1 1.1 NA
bit 22.2 43.7 0.9 NA
bitwhich 31.7 62.1 1.9 NA
which NA NA NA NA
ri NA NA NA NA

% time assigning with ‘which’

% time of logical
coin often rare chunk
logical 20.8 40.2 0.7 NA
bit 16.0 38.0 0.5 NA
bitwhich 36.7 28.9 2.3 NA
which NA NA NA NA
ri NA NA NA NA

% time Boolean NOT

% time for Boolean NOT
coin often rare chunk
logical 18.3 19.5 18.2 18.3
bit 0.7 0.7 0.8 0.8
bitwhich 10.4 0.8 0.8 1.8
which NA NA NA NA
ri NA NA NA NA

% time Boolean AND

% time for Boolean &
coin often rare chunk
logical 97.4 28.9 27.1 25.5
bit 4.5 4.8 4.7 4.7
bitwhich 18.1 5.6 4.9 5.9
which NA NA NA NA
ri NA NA NA NA

% time Boolean OR

% time for Boolean |
coin often rare chunk
logical 100.0 33.3 25.1 24.9
bit 4.6 4.6 4.6 4.4
bitwhich 38.4 4.7 5.5 7.6
which NA NA NA NA
ri NA NA NA NA

% time Boolean EQUALITY

% time for Boolean ==
coin often rare chunk
logical 26.1 26.2 26.1 26.0
bit 4.7 4.8 4.5 4.6
bitwhich 14.8 4.7 4.8 5.7
which NA NA NA NA
ri NA NA NA NA

% time Boolean XOR

% time for Boolean !=
coin often rare chunk
logical 24.8 24.8 24.8 24.8
bit 4.6 4.6 4.5 4.7
bitwhich 15.7 4.8 4.8 5.4
which NA NA NA NA
ri NA NA NA NA

% time Boolean SUMMARY

% time for Boolean summary
coin often
logical 84.6 17.9
bit 3.8 3.6

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 126.4 173.9
sortunique 76.3 39.5
unsorted data relative to R’s sort
small big
sort 26.8 36.2
sortunique 16.6 12.5

% time for unique

sorted data relative to R
small big
bit 127.2 23.5
merge 24.4 9.7
sort 0.0 0.0
unsorted data relative to R
small big
bit 106.0 16.7
merge 123.0 72.7
sort 100.7 64.5

% time for duplicated

sorted data relative to R
small big
bit 261.4 26.9
merge 28.9 10.2
sort 0.0 0.0
unsorted data relative to R
small big
bit 213.9 11.7
merge 147.7 53.1
sort 120.8 47.7

% time for anyDuplicated

sorted data relative to R
small big
bit 150.2 25.7
merge 18.5 4.0
sort 0.0 0.0
unsorted data relative to R
small big
bit 155.9 19.3
merge 161.1 84.9
sort 142.6 81.0

% time for sumDuplicated

sorted data relative to R
small big
bit 121.6 19.2
merge 20.7 7.9
sort 0.0 0.0
unsorted data relative to R
small big
bit 118.7 17.9
merge 123.7 84.2
sort 104.5 74.7

% time for match

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit NA NA NA NA
merge 26.2 0 10.1 4.5
sort 0.0 0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit NA NA NA NA
merge 217.7 82.7 84.6 81.4
sort 192.6 82.7 81.5 77.4

% time for in

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 173.2 6.2 19.8 18.4
merge 22.8 0.0 7.9 4.5
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 155.6 5.6 7.7 14.3
merge 195.3 80.6 76.6 74.0
sort 173.3 80.6 73.5 70.2

% time for notin

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 255.4 4.1 14.3 19
merge 21.1 0.0 9.5 4
sort 0.0 0.0 0.0 0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 379.1 5.7 6.8 13.3
merge 191.9 79.6 67.0 69.2
sort 167.9 79.6 62.5 66.0

% time for union

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 34.5 14.8 13.9 11.0
merge 23.9 6.3 6.0 7.9
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 30.8 12.0 13.9 13.3
merge 61.0 37.5 42.5 43.7
sort 38.8 31.7 35.9 35.5

% time for intersect

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 31.3 4.3 4.9 7.8
merge 10.0 0.0 0.0 4.0
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 27.9 4.8 4.5 6.3
merge 51.4 36.8 34.1 38.4
sort 41.6 36.8 34.0 34.0

% time for setdiff

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 28.4 2.2 14.7 7.7
merge 9.4 0.0 5.8 4.1
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 25.3 2.6 13.1 5.9
merge 52.2 36.0 39.1 39.3
sort 43.1 36.0 33.1 35.3

% time for symdiff

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 19.7 7.8 6.9 7.4
merge 4.0 2.2 1.9 2.4
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 20.8 6.5 6.9 6.0
merge 24.5 15.0 15.0 20.0
sort 20.2 12.7 12.7 17.7

% time for setequal

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 54.0 54.2 9.1 10.2
merge 12.4 12.1 4.4 4.9
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 61.3 52.2 8.6 9.1
merge 78.0 26089.7 25.8 48.8
sort 64.3 26077.4 20.6 43.2

% time for setearly

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 65.2 3.5 16.3 12.0
merge 14.1 0.0 0.1 5.8
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 61.9 2.4 5.2 8.6
merge 75.8 36.9 80.6 46.1
sort 61.6 36.9 80.5 40.9