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

<%@string fcnname="allocVector"%>
<% fcnname <- "<%@string name="fcnname"%>" %>
<%@meta title="${fcnname}() benchmarks"%>
<%@meta author="Henrik Bengtsson"%>
<%@meta date="2014-11-09"%>

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


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

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

## Alternative methods

* vector() + assignment
* rep()
* matrix() + as.vector()

where
```r
<%=withCapture({
allocVector_R1 <- function(length, value = NA) {
  x <- vector(mode = typeof(value), length = length)
  if (!is.finite(value) || value != 0) x[] <- value
  x
} # allocVector_R1()

allocVector_R2 <- function(length, value = NA) {
  x <- matrix(data = value, nrow = length, ncol = 1L)
  as.vector(x)
} # allocVector_R2()
})%>
```


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

## Data type "<%=mode%>"
### Data
```r
<%=withCapture({
<%@include file="R/random-vectors.R"%>
data <- rvectors(mode = mode)
values <- list(zero = 0, one = 1, "NA" = NA_real_)
if (mode != "double")
  values <- lapply(values, FUN = function(x) { storage.mode(x) <- mode; x })
})%>
```

### Results

<% for (ii in seq_along(data)) { %>
<%
dataLabel <- names(data)[ii]
x <- data[[dataLabel]]
gc()
%>
### <%=dataLabel%> matrix

<% for (value in values) { %>

<%
valueLabel <- as.character(value)
mprintf("%s: %s, value=%s\n", mode, dataLabel, valueLabel)
%>

```r
<%=withCapture({
n <- length(data[[.dataLabel.]])
str(value)
})%>
```
<% gc() %>

```r
<%=withCapture({
stats <- microbenchmark(
 "allocVector"         = allocVector(length = n, value = value),
 "rep"                 = rep(value, times = n),
 "allocVector_R1"      = allocVector_R1(length = n, value = value),
 "allocVector_R2"      = allocVector_R2(length = n, value = value),
  unit = "ms"
)
})%>
```

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

<% } # for (value in values) %>

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

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

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


<%---------------------------------------------------------------------------
HISTORY:
2014-11-01
o Created.
---------------------------------------------------------------------------%>
