implicit return? [🇷 for BE/BA]
Hi Hötzi,
Try this:
note I am passing Len to f1 even though it is not used there in order to give the function call the same overhead, otherwise the comparison might be called unfair.
On my system f2 is a lot faster, so the advantage of runif may be solely syntactic and not in any way a true vectorisation advantage (easier but not more efficient).
❝ Use ifelse()
for vectorized conditions. See this thread.
Try this:
library(microbenchmark)
x=round(runif(100),0)
Len=100
f1=function(x, Len)
{
ifelse ((x==0), "foo", "bar")
}
f1(x, Len)
f2=function(x, Len)
{
rslt=rep("foo", Len)
for (i in 1:Len)
if (x[i]) rslt[i]="bar"
return(rslt)
}
f2(x, Len)
res <- microbenchmark(
f1(round(runif(Len),0), Len),
f2(round(runif(Len),0), Len),
times=3000L)
print(res)
note I am passing Len to f1 even though it is not used there in order to give the function call the same overhead, otherwise the comparison might be called unfair.
On my system f2 is a lot faster, so the advantage of runif may be solely syntactic and not in any way a true vectorisation advantage (easier but not more efficient).
—
Pass or fail!
ElMaestro
Pass or fail!
ElMaestro
Complete thread:
- R Inferno Helmut 2019-04-26 16:03 [🇷 for BE/BA]
- full conditions! mittyri 2019-04-26 16:20
- full conditions! Helmut 2019-04-26 16:41
- full conditions! d_labes 2019-04-27 14:29
- magik of R implicit return mittyri 2019-04-27 22:23
- magik of R implicit return ElMaestro 2019-04-27 22:34
- implicit return? d_labes 2019-04-28 19:36
- implicit return? ElMaestro 2019-04-28 20:36
- implicit return? Helmut 2019-04-28 20:50
- implicit return? ElMaestro 2019-04-28 21:16
- implicit return? Helmut 2019-04-28 23:38
- implicit return? ElMaestro 2019-04-29 09:40
- implicit return? Helmut 2019-04-29 10:41
- implicit return?ElMaestro 2019-04-29 12:55
- built-in ifelse mittyri 2019-04-30 13:05
- implicit return?ElMaestro 2019-04-29 12:55
- implicit return? Helmut 2019-04-29 10:41
- implicit return? ElMaestro 2019-04-29 09:40
- implicit return? Helmut 2019-04-28 23:38
- implicit return? ElMaestro 2019-04-28 21:16
- implicit return? d_labes 2019-04-28 19:36
- magik of R implicit return ElMaestro 2019-04-27 22:34
- magik of R implicit return mittyri 2019-04-27 22:23
- full conditions! mittyri 2019-04-26 16:20