I was wondering if calling a function from lapply would be faster than creating a function inside lapply.
I tried to create a small reprex but feel free to add your input.
library(data.table)
library(compiler)
library(microbenchmark)
f <- cmpfun(
function(a,b){
if(a < 0 || b < 0 ){
"useless"
} else if(a>b){
"greater"
}else if( a == b){
"lesser"
} else{
"equal"
}
})
dt <- data.table(a = rnorm(20000),
b = rnorm(20000))
This is a compiled functions it should be faster on the run. So if I call this from inside an lapply function it should be faster than the normal function. So i tried it
microbenchmark(
dt[,lapply(.SD$a, function(a,b){
if(a < 0 || b < 0 ){
"useless"
} else if(a>b){
"greater"
}else if( a == b){
"lesser"
} else{
"equal"
}
}, .SD$b)]
,times = 5L)
This gave me the results
min lq mean median uq max neval
6.323885 6.375043 6.446601 6.401394 6.565713 6.566967 5
While calling the compiled function like this
microbenchmark(
dt[,lapply(.SD$a,f, .SD$b)]
,times = 5L)
gave me exactly the same output
Unit: seconds
expr min lq mean median uq max neval
dt[, lapply(.SD$a, f, .SD$b)] 6.221426 6.230694 6.263112 6.237644 6.269459 6.356337 5
Does anybody has any idea what is happening here????
any input is appreciated. It is just for my own understanding.