<html><body><div style="color:#000; background-color:#fff; font-family:Courier New, courier, monaco, monospace, sans-serif;font-size:10pt"><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif;"><span style="font-size: 10px;">Hi,</span></div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; color: rgb(0, 0, 0); font-size: 10px; background-color: transparent; font-style: normal;"><span style="font-size: 10px;"><br></span></div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; color: rgb(0, 0, 0); font-size: 12px; background-color: transparent; font-style: normal;"><span style="font-size: 10px;">I'm trying to implement a faster version of apply(x, 1L, FUN) in C++ but found some strange behaviors.</span></div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; color: rgb(0, 0, 0); font-size: 10px; background-color: transparent; font-style:
 normal;"><span style="font-size: 10px;"><br></span></div><div style="background-color: transparent;"><span style="font-size: 12px;">// [[Rcpp::export]]</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">NumericVector rowApply(NumericMatrix& x, const Function& FUN)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">{</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">  int n = x.nrow();</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">  NumericVector result = no_init(n);</span></div><div style="background-color: transparent;"><span style="font-size: 12px;"><br></span></div><div style="background-color: transparent;"><span style="font-size: 12px;">  for (int r = 0; r < n; r++) {</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">   
 result[r] = as<double>(FUN(x(r, _) ) );</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">  }</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">  return result;</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">}</span></div><div style="background-color: transparent;"><span style="font-size: 12px;"><br></span></div><div style="background-color: transparent;"><span style="font-size: 12px;">> M <- matrix(1:15, nrow=3);</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">> apply(M, 1L, mean)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">[1] 7 8 9</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">> rowApply(M, mean);</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">[1] 7 8
 9</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">> identical(rowApply(M, mean), apply(M, 1L, mean));</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">[1] TRUE</span></div><div style="background-color: transparent;"><span style="font-size: 12px;"></span></div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 12px; background-color: transparent;"><br></div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 12px; background-color: transparent; color: rgb(0, 0, 0); font-style: normal;"><br></div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 12px; background-color: transparent; color: rgb(0, 0, 0); font-style: normal;">so far so good, with rowApply() is identical to apply(, 1L, ), and the speed of rowApply() (in these cases) are faster as measured by
 benchmark().</div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 12px; background-color: transparent; color: rgb(0, 0, 0); font-style: normal;"><br></div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 12px; background-color: transparent; color: rgb(0, 0, 0); font-style: normal;"><br></div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 12px; background-color: transparent; color: rgb(0, 0, 0); font-style: normal;">HOWEVER, when I Rprof() my own project that uses rowApply(), I found a lot of time WASTED in calling tryCatch(), tryCatchList(), tryCatchOne(), doTryCatch() and evalq().</div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 12px; background-color: transparent; color: rgb(0, 0, 0); font-style: normal;"><br></div><div style="font-family: 'Courier New', courier, monaco,
 monospace, sans-serif; font-size: 12px; background-color: transparent; color: rgb(0, 0, 0); font-style: normal;">so, I do this:</div><div style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 12px; background-color: transparent; color: rgb(0, 0, 0); font-style: normal;"><br></div><div style="background-color: transparent;"><span style="font-size: 12px;">> debug(tryCatch);</span></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><span style="font-size: 12px;"><br></span></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><span style="font-size: 12px;">then,</span></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New',
 courier, monaco, monospace, sans-serif; font-style: normal;"><span style="font-size: 12px;"><br></span></div><div style="background-color: transparent;"><span style="font-size: 12px;">> apply(M, 1L, mean)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;"></span></div><div style="background-color: transparent;"><span style="font-size: 12px;">[1] 7 8 9</span></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><span style="font-size: 12px;"><br></span></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><span style="font-size: 12px;">we see that apply(, 1L, ) runs just fine (without calling things like tryCatch() ).</span></div><div style="background-color: transparent;
 color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><span style="font-size: 12px;"><br></span></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><span style="font-size: 12px;"><br></span></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;">UNFORTUNATELY:</div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><span style="font-size: 12px;"><br></span></div><div style="background-color: transparent;"><span style="font-size: 12px;">> rowApply(M, mean);</span></div><div style="background-color: transparent;"><span
 style="font-size: 12px;">debugging in: tryCatch(evalq(function (x, ...)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">UseMethod("mean")(c(1, 4, 7, 10, 13)), <environment>), error = .rcpp_error_recorder)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">debug: {</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    tryCatchList <- function(expr, names, parentenv, handlers) {</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        nh <- length(names)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        if (nh > 1L)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">            tryCatchOne(tryCatchList(expr, names[-nh],
 parentenv,</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">                handlers[-nh]), names[nh], parentenv, handlers[[nh]])</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        else if (nh == 1L)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">            tryCatchOne(expr, names, parentenv, handlers[[1L]])</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        else expr</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    }</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    tryCatchOne <- function(expr, name, parentenv, handler) {</span></div><div style="background-color:
 transparent;"><span style="font-size: 12px;">        doTryCatch <- function(expr, name, parentenv, handler) {</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">            .Internal(.addCondHands(name, list(handler), parentenv,</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">                environment(), FALSE))</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">            expr</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        }</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        value <- doTryCatch(return(expr), name, parentenv, handler)</span></div><div style="background-color:
 transparent;"><span style="font-size: 12px;">        if (is.null(value[[1L]])) {</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">            msg <- .Internal(geterrmessage())</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">            call <- value[[2L]]</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">            cond <- simpleError(msg, call)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        }</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        else cond <- value[[1L]]</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">       
 value[[3L]](cond)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    }</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    if (!missing(finally))</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        on.exit(finally)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    handlers <- list(...)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    classes <- names(handlers)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    parentenv <- parent.frame()</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    if (length(classes) != length(handlers))</span></div><div style="background-color:
 transparent;"><span style="font-size: 12px;">        stop("bad handler specification")</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    tryCatchList(expr, classes, parentenv, handlers)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">}</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">Browse[2]></span></div><div style="background-color: transparent;"><span style="font-size: 12px;">debug: tryCatchList <- function(expr, names, parentenv, handlers) {</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    nh <- length(names)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    if (nh > 1L)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">       
 tryCatchOne(tryCatchList(expr, names[-nh], parentenv,</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">            handlers[-nh]), names[nh], parentenv, handlers[[nh]])</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    else if (nh == 1L)</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">        tryCatchOne(expr, names, parentenv, handlers[[1L]])</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">    else expr</span></div><div style="background-color: transparent;"><span style="font-size: 12px;">}</span></div><div style="background-color: transparent;"><span style="font-size: 12px;"></span></div><div style="background-color: transparent;"><span style="font-size: 12px;">Browse[2]></span></div><div style="background-color: transparent;
 color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><span style="font-size: 12px;"><br></span></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><br></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><span style="background-color: transparent;">that is, tryCatch() is TRIGGERED !!!</span><br></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;">so, is it the problem of the implementation of rowApply()? </div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier
 New', courier, monaco, monospace, sans-serif; font-style: normal;">is there any way around that I could prevent the triggering of tryCatch()?</div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;"><br></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;">Many thanks,</div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 12px; font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-style: normal;">Thomas</div>  </div></body></html>