Reputation: 1981
I have two vectors r
and s
. I want to find the outer difference of these two arrays and not be negative like the following
r = rnorm(100000)
s = c(0.02, 0.04, 0.3, 0.43, 0.5, 0.7, 0.8, 0.9)
res = t(pmax(outer(r, s, "-"), 0))
system.time({
res = t(pmax(outer(r, s, "-"), 0))
})
## system elapsed
## 0.05 0.00 0.05
or
system.time({
x = pmax(r - rep(s, each = length(r)), 0)
res = matrix(x, nrow = length(s), byrow = TRUE)
})
## system elapsed
## 0.05 0.00 0.05
How can I get faster the result x in R?
Upvotes: 3
Views: 245
Reputation: 59970
I get slightly faster performance by running the outer
function seperately and the subsetting zero < 0
entries like this...
res1 <- t( outer( r , s , "-" ) )
res1[ res1 < 0 ] <- 0
But if you want even more speed then try using Rcpp
. It's easy enough, just run the following code snippet....
if( ! require( Rcpp ) ) install.packages( "Rcpp" )
Rcpp::cppFunction( '
NumericMatrix gtzero(NumericVector r , NumericVector s){
int cols = r.size();
int rows = s.size();
NumericMatrix out(rows, cols);
for( int i = 0; i < cols; i++){
NumericMatrix::Column ncol = out( _, i );
ncol = ifelse( r[i] - s > 0 , r[i] - s , 0 );
}
return out;
}
')
Then use the function like this:
gtzero( r , s )
This turns out to be about 6 times faster than using outer
and pmax
and 3 times faster than outer
then [
subsetting:
require( microbenchmark )
bm <- microbenchmark( eval( rose.baseR ) , eval( simon.baseR ) , eval( simon.Rcpp ) )
print( bm , "relative" , order = "median" , digits = 2 )
#Unit: relative
# expr min lq median uq max neval
# eval(simon.Rcpp) 1 1.0 1.0 1.0 1.0 100
# eval(simon.baseR) 3 3.1 3.2 3.2 1.5 100
# eval(rose.baseR) 3 3.4 6.0 5.9 1.8 100
And gives the exact same result:
identical( res0 , res2 )
#[1] TRUE
The following expressions were evaluated:
set.seed(123)
r = rnorm(100000)
s = c(0.02, 0.04, 0.3, 0.43, 0.5, 0.7, 0.8, 0.9)
rose.baseR <- quote({
res0 <- t(pmax(outer(r, s, "-"), 0))
})
simon.baseR <- quote({
res1 <- outer( r , s , "-" )
res1[ res1 < 0 ] <- 0
})
simon.Rcpp <- quote({
res2 <- gtzero(r,s)
})
Upvotes: 2
Reputation: 132706
Following @thelatemail's comment:
fun1 <- function(r,s) t(pmax(outer(r, s, "-"), 0))
fun2 <- function(r,s) {
x = pmax(r - rep(s, each = length(r)), 0)
matrix(x, nrow = length(s), byrow = TRUE)
}
fun3 <- function(r,s) {
dr <- length(r)
ds <- length(s)
R <- rep(s, rep.int(length(r), length(s)))
S <- rep(r, times = ceiling(length(s)/length(r)))
res <- pmax(S - R, 0)
dim(res) <- c(dr, ds)
t(res)
}
library(microbenchmark)
microbenchmark(res1 <- fun1(r,s),
res2 <- fun2(r,s),
res3 <- fun3(r,s),
times=20)
# Unit: milliseconds
# expr min lq median uq max neval
# res1 <- fun1(r, s) 43.28387 46.68182 66.03417 78.78109 83.75569 20
# res2 <- fun2(r, s) 50.52941 54.36576 56.77067 60.87218 91.14043 20
# res3 <- fun3(r, s) 34.18374 35.37835 37.97405 40.10642 70.78626 20
identical(res1, res3)
#[1] TRUE
Upvotes: 1