# NOT RUN {
x <- as.bit(c(TRUE, TRUE))
all(x)
any(x)
min(x)
max(x)
range(x)
sum(x)
summary(x)
x <- as.bitwhich(c(TRUE, TRUE))
all(x)
any(x)
min(x)
max(x)
range(x)
sum(x)
summary(x)
# }
# NOT RUN {
n <- .Machine$integer.max
x <- !bit(n)
N <- 1000000L # batchsize
B <- n %/% N # number of batches
R <- n %% N # rest
message("Batched sum (52.5 sec on Centrino duo)")
system.time({
s <- 0L
for (b in 1:B){
s <- s + sum(x[((b-1L)*N+1L):(b*N)])
}
if (R)
s <- s + sum(x[(n-R+1L):n])
})
message("Batched sum saving repeated memory allocation for the return vector
(44.4 sec on Centrino duo)")
system.time({
s <- 0L
l <- logical(N)
for (b in 1:B){
.Call("R_bit_extract", x, length(x), ((b-1L)*N+1L):(b*N), l, PACKAGE = "bit")
s <- s + sum(l)
}
if (R)
s <- s + sum(x[(n-R+1L):n])
})
message("C-coded sum (3.1 sec on Centrino duo)")
system.time(sum(x))
# }
Run the code above in your browser using DataLab