2025-01-12 00:52:51 +08:00

98 lines
1.7 KiB
Plaintext

<%@include file="includes/setup.md.rsp"%>
<%@string fcnname="product"%>
<% fcnname <- "<%@string name="fcnname"%>" %>
<%@meta title="${fcnname}() benchmarks"%>
<%@meta author="Henrik Bengtsson"%>
<%@meta date="2014-06-04"%>
<%@include file="${header}"%>
# <%@meta name="title"%>
This report benchmark the performance of <%=fcnname%>() against alternative methods.
## Alternative methods
* product_R()
* prod()
where
```r
<%=withCapture({
product_R <- function(x, na.rm = FALSE, ...) {
# Nothing todo?
if (length(x) == 0L) return(0);
# Any missing values?
if (na.rm) {
x <- x[!is.na(x)];
}
# Any zeros?
if (is.integer(x) && any(x == 0)) return(0);
# Calculate product via logarithmic sum
sign <- if (sum(x < 0) %% 2 == 0) +1 else -1;
x <- abs(x);
x <- log(x);
x <- sum(x, na.rm = FALSE);
x <- exp(x);
y <- sign*x;
y;
} # product_R()
})%>
```
## Data
```r
<%=withCapture({
<%@include file="R/random-vectors.R"%>
data <- rvectors(mode = "double")
data <- data[1:4]
})%>
```
## Results
<% for (ii in seq_along(data)) { %>
<%
dataLabel <- names(data)[ii]
message(dataLabel)
x <- data[[dataLabel]]
gc()
%>
### <%=dataLabel%> vector
```r
<%=withCapture({
x <- data[[.dataLabel.]]
gc()
stats <- microbenchmark(
product = product(x, na.rm = FALSE),
product_R = product_R(x, na.rm = FALSE),
prod = prod(x, na.rm = FALSE),
unit = "ms"
)
})%>
```
<% benchmarkResults(stats, tags=dataLabel) %>
<% } # for (ii ...) %>
<%@include file="${footer}"%>
<%---------------------------------------------------------------------------
HISTORY:
2014-06-02
o Created.
---------------------------------------------------------------------------%>