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 27 26.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 19.2 6.2 6.0 NA
bit 18.1 18.2 18.0 NA
bitwhich 26.8 53.5 6.2 NA
which NA NA NA NA
ri NA NA NA NA

% time assigning

% time of logical
coin often rare chunk
logical 39.7 40.2 40.8 NA
bit 80.1 12.8 12.9 NA
bitwhich 182.6 42.6 42.4 NA
which NA NA NA NA
ri NA NA NA NA

% time subscripting with ‘which’

% time of logical
coin often rare chunk
logical 21.4 35.0 0.9 NA
bit 22.2 43.2 0.7 NA
bitwhich 31.0 60.9 1.7 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.5 0.7 NA
bit 15.8 38.1 0.5 NA
bitwhich 36.0 28.7 2.4 NA
which NA NA NA NA
ri NA NA NA NA

% time Boolean NOT

% time for Boolean NOT
coin often rare chunk
logical 18.4 18.6 18.4 18.6
bit 0.8 0.7 0.7 0.7
bitwhich 12.8 0.8 0.8 1.9
which NA NA NA NA
ri NA NA NA NA

% time Boolean AND

% time for Boolean &
coin often rare chunk
logical 97.2 28.7 26.9 25.5
bit 4.7 4.3 4.7 4.6
bitwhich 17.9 5.2 4.9 6.1
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 24.9 25.2
bit 4.7 4.5 4.6 4.2
bitwhich 38.5 4.7 5.0 7.2
which NA NA NA NA
ri NA NA NA NA

% time Boolean EQUALITY

% time for Boolean ==
coin often rare chunk
logical 26.0 26.1 26.2 26.2
bit 4.7 4.5 4.4 4.3
bitwhich 15.2 4.5 4.4 5.2
which NA NA NA NA
ri NA NA NA NA

% time Boolean XOR

% time for Boolean !=
coin often rare chunk
logical 24.7 24.8 24.7 24.8
bit 4.8 4.6 4.9 4.3
bitwhich 14.3 4.5 4.5 5.4
which NA NA NA NA
ri NA NA NA NA

% time Boolean SUMMARY

% time for Boolean summary
coin often
logical 85.3 16.9
bit 3.5 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 148.1 191.3
sortunique 87.7 37.2
unsorted data relative to R’s sort
small big
sort 19.0 31.4
sortunique 16.1 13.1

% time for unique

sorted data relative to R
small big
bit 128.1 22.0
merge 24.7 10.9
sort 0.0 0.0
unsorted data relative to R
small big
bit 120.2 17.3
merge 133.9 75.2
sort 108.7 65.1

% time for duplicated

sorted data relative to R
small big
bit 236.6 26.4
merge 31.6 10.0
sort 0.0 0.0
unsorted data relative to R
small big
bit 212.2 18.7
merge 148.1 85.2
sort 119.1 76.4

% time for anyDuplicated

sorted data relative to R
small big
bit 163.3 31.8
merge 20.6 4.3
sort 0.0 0.0
unsorted data relative to R
small big
bit 155.7 18.8
merge 164.9 81.1
sort 144.0 77.8

% time for sumDuplicated

sorted data relative to R
small big
bit 123.9 24.9
merge 21.1 10.6
sort 0.0 0.0
unsorted data relative to R
small big
bit 113.3 17.5
merge 120.7 81.3
sort 100.7 72.0

% time for match

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit NA NA NA NA
merge 22.7 0 22.1 5.5
sort 0.0 0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit NA NA NA NA
merge 206.2 75.3 79.7 79.6
sort 181.9 75.3 71.3 75.9

% time for in

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 165.8 7 14.0 23
merge 21.2 0 15.9 5
sort 0.0 0 0.0 0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 166.0 5.4 6.5 15.2
merge 186.8 76.8 68.8 77.6
sort 165.8 76.8 61.5 74.0

% time for notin

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 213.2 6.7 12.1 21.4
merge 18.8 0.0 4.3 4.6
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 265.7 5.1 6.4 13.0
merge 178.9 70.5 58.0 66.4
sort 157.7 70.5 55.8 63.3

% time for union

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 33.6 23.9 22.4 17.1
merge 23.1 14.3 13.6 11.8
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 46.4 13.8 21.2 12.0
merge 65.8 33.2 50.8 43.1
sort 41.0 24.5 37.4 34.1

% time for intersect

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 29.3 5.4 6 9.8
merge 8.6 0.0 0 5.0
sort 0.0 0.0 0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 28.5 4.8 4.4 7.8
merge 47.7 35.4 33.1 38.0
sort 38.8 35.3 33.1 33.7

% time for setdiff

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 28.4 2.8 19.4 9.3
merge 9.0 0.0 15.6 5.1
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 25.9 2.8 12.2 6.3
merge 51.8 39.2 41.6 41.8
sort 42.9 39.2 30.7 37.5

% time for symdiff

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 22.1 11.2 12.6 8.9
merge 3.9 4.9 5.5 2.9
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 16.7 8.8 9.5 6.3
merge 18.5 16.9 18.2 20.5
sort 15.2 12.5 13.4 18.2

% time for setequal

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 66.8 65.0 14.1 15.9
merge 14.4 13.7 6.7 7.7
sort 0.0 0.0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 56.2 53.7 8.4 9.8
merge 71.0 26591.2 24.7 51.6
sort 56.4 26579.2 19.6 45.7

% time for setearly

sorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 59.5 4 12.6 15.6
merge 14.2 0 0.1 7.5
sort 0.0 0 0.0 0.0
unsorted data relative to R
smallsmall smallbig bigsmall bigbig
bit 39.1 2.4 4.7 9.0
merge 47.5 35.7 71.7 47.4
sort 38.0 35.6 71.6 42.0