Skip to content
Prev 170721 / 398506 Next

Alternate to for-loop

Patrick Burns wrote:
... and the benchmark was unfair, in that wacek forgot to take means in
f.rep.  which, as far as i can see, does not change the results
substantially.
the results will in general depend on the m:n ratio.  i have
specifically picked the valuies to show it's not *necessarily* the case
that f.pat is much faster than f.rep, even if in some cases it might be. 

with n sufficiently larger than m, say n:m = 10^5, pat's solution is
indeed much faster, while the opposite (n:m = 10^-5) leads to equal
performance.  so you'd be justified to say that f.pat is no worse than
f.rep in the general case.
haven't examined the sources and thus unsure, but my guess is that part
of the overhead lies in that replicate calls sapply, sapply calls
lapply, with additional testing, and then lapply calls internal lapply. 
(it might be that the for-looping is actually done in c and is not
guilty -- correct me if i'm severely off.)

the point of the exercise was to give a humble hint:  if you provide an
advice such as the one discussed here, it will, in  general, be helpful
to hint in which cases it is worth using it instead of an arguably more
code-elegant one.

vQ