Skip to content

Commit

Permalink
rowSums2() is now significantly faster for larger matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Jun 22, 2024
1 parent 6ff408e commit 72b7aaf
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: matrixStats
Version: 1.3.0-9001
Version: 1.3.0-9002
Depends:
R (>= 2.12.0)
Suggests:
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Version (development version)

* ...
## Performance

* `rowSums2()` is now significantly faster for larger matrices.


# Version 1.3.0 [2024-04-10]
Expand Down
2 changes: 1 addition & 1 deletion src/rowSums2.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ SEXP rowSums2(SEXP x, SEXP dim, SEXP rows, SEXP cols, SEXP naRm, SEXP hasNA, SEX
*/

/* R allocate a double vector of the desired length */
PROTECT(ans = allocVector(REALSXP, byrow ? nrows: ncols));
PROTECT(ans = allocVector(REALSXP, byrow ? nrows : ncols));

/* Double matrices are more common to use. */
if (isReal(x)) {
Expand Down
8 changes: 4 additions & 4 deletions src/rowSums2_lowlevel_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void CONCAT_MACROS(rowSums2, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nrow, R_xlen_t
rowSum = LDOUBLE_ALLOC(nrows);
/* Ensures that all elements of array are intialized to zero,
* this is VERY important */
memset(rowSum, 0, nrows*sizeof(LDOUBLE));
memset(rowSum, 0, nrows* sizeof(LDOUBLE));
}

for (jj=0; jj < ncols; jj++) {
Expand All @@ -58,7 +58,7 @@ void CONCAT_MACROS(rowSums2, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nrow, R_xlen_t
} else if (!colsHasNA) {
colOffset = cols[jj] * nrow;
} else {
colOffset = R_INDEX_OP(cols[jj], *, nrow,1,1);
colOffset = R_INDEX_OP(cols[jj], *, nrow, 1, 1);
}
for (ii=0; ii < nrows; ii++) {
if (!colsHasNA && norows) {
Expand All @@ -73,9 +73,9 @@ void CONCAT_MACROS(rowSums2, X_C_SIGNATURE)(X_C_TYPE *x, R_xlen_t nrow, R_xlen_t
value = x[idx];
} else {
if (norows) {
idx = R_INDEX_OP(colOffset, +, ii,1,1);
idx = R_INDEX_OP(colOffset, +, ii, 1, 1);
} else {
idx = R_INDEX_OP(colOffset, +, rows[ii],1,1);
idx = R_INDEX_OP(colOffset, +, rows[ii], 1, 1);
}
value = R_INDEX_GET(x, idx, X_NA, 1);
}
Expand Down

0 comments on commit 72b7aaf

Please sign in to comment.