<%@include file="includes/setup.md.rsp"%>

<%@string fcnname="binCounts"%>
<% fcnname <- "<%@string name="fcnname"%>" %>
<%@meta title="${fcnname}() benchmarks"%>
<%@meta author="Henrik Bengtsson"%>
<%@meta date="2014-05-25"%>

<%@include file="${header}"%>


# <%@meta name="title"%>

This report benchmark the performance of <%=fcnname%>() against alternative methods.

## Alternative methods

* hist()

as below

```r
<%=withCapture({
hist <- graphics::hist
binCounts_hist <- function(x, bx, right = FALSE, ...) {
  hist(x, breaks = bx, right = right, include.lowest = TRUE, plot = FALSE)$counts
}
})%>
```

<% for (mode in c("integer", "double")) { %>

## Data type "<%=mode%>"

### Non-sorted simulated data
```r
<%=withCapture({
set.seed(0xBEEF)
nx <- 100e3 # Number of data points
xmax <- 0.01*nx
x <- runif(nx, min = 0, max = xmax)
storage.mode(x) <- mode
str(x)

# Uniformely distributed bins
nb <- 10e3 # Number of bins
bx <- seq(from = 0, to = xmax, length.out = nb+1L)
bx <- c(-1, bx, xmax+1)
})%>
```

### Results

<% benchmark <- function() { %>
<% dataLabel <- if (is.unsorted(x)) "unsorted" else "sorted" %>
<% mprintf("%s: %s\n", mode, dataLabel) %>
```r
<%=withCapture({
gc()

stats <- microbenchmark(
 binCounts = binCounts(x, bx = bx),
 hist      = binCounts_hist(x, bx = bx),
 unit = "ms"
)
})%>
```

<% benchmarkResults(stats, tags=c(mode, dataLabel)) %>

<%
# Sanity checks
n0 <- binCounts_hist(x, bx = bx)
n1 <- binCounts(x, bx = bx)
stopifnot(identical(n1, n0))
n1r <- rev(binCounts(-x, bx = rev(-bx), right = TRUE))
stopifnot(identical(n1r, n1))
%>
<% } # benchmark() %>

<% benchmark() %>


### Sorted simulated data
```r
<%=withCapture({
  x <- sort(x)
})%>
```
<% benchmark() %>


<% } # for (mode ...) %>


<%@include file="${footer}"%>


<%---------------------------------------------------------------------------
HISTORY:
2014-06-02
o Restructured.
2014-05-25
o Created.
---------------------------------------------------------------------------%>
