117 lines
2.2 KiB
Plaintext
Raw Normal View History

2025-01-12 00:52:51 +08:00
<%@include file="includes/setup.md.rsp"%>
<%@string fcnname="mean2"%>
<% fcnname <- "<%@string name="fcnname"%>" %>
<%@meta title="${fcnname}() benchmarks"%>
<%@meta author="Henrik Bengtsson"%>
<%@meta date="2014-11-02"%>
<%@include file="${header}"%>
# <%@meta name="title"%>
This report benchmark the performance of <%=fcnname%>() against alternative methods.
## Alternative methods
* mean() + [()
* mean.default() + [() - avoids method dispatching
as below
```r
<%=withCapture({
mean2_R_v1 <- function(x, na.rm = FALSE, idxs) {
mean(x[idxs], na.rm = na.rm)
}
})%>
```
and
```r
<%=withCapture({
mean2_R_v2 <- function(x, na.rm = FALSE, idxs) {
mean.default(x[idxs], na.rm = na.rm)
}
})%>
```
<% for (mode in c("integer", "double")) { %>
## Data type "<%=mode%>"
### Data
```r
<%=withCapture({
<%@include file="R/random-vectors.R"%>
data <- rvectors(mode = mode)
##data <- data[1:3]
})%>
```
### Results
<% for (ii in seq_along(data)) { %>
<%
dataLabel <- names(data)[ii]
mprintf("%s: %s\n", mode, dataLabel)
x <- data[[dataLabel]]
gc()
%>
### <%=dataLabel%> vector
#### All elements
```r
<%=withCapture({
x <- data[[.dataLabel.]]
gc()
stats <- microbenchmark(
"mean2" = mean2(x, refine = TRUE),
"mean2_no_refine" = mean2(x, refine = FALSE),
"mean" = mean(x),
"mean.default" = mean.default(x),
unit = "ms"
)
})%>
```
<% benchmarkResults(stats, tags=c(dataLabel, "all")) %>
<% for (subset in c(0.2, 0.4, 0.8)) { %>
#### A <%=sprintf("%g", 100*subset)%>% subset
```r
<%=withCapture({
x <- data[[.dataLabel.]]
subset
idxs <- sort(sample(length(x), size = subset*length(x), replace = FALSE))
gc()
stats <- microbenchmark(
"mean2" = mean2(x, idxs = idxs, refine = TRUE),
"mean2_no_refine" = mean2(x, idxs = idxs, refine = FALSE),
"mean+[()" = mean2_R_v1(x, idxs = idxs),
"mean.default+[()" = mean2_R_v2(x, idxs = idxs),
unit = "ms"
)
})%>
```
<% benchmarkResults(stats, tags=c(mode, dataLabel, subset)) %>
<% } # for (subset in ...) %>
<% } # for (ii ...) %>
<% } # for (mode ...) %>
<%@include file="${footer}"%>
<%---------------------------------------------------------------------------
HISTORY:
2014-11-02
o Created.
---------------------------------------------------------------------------%>