[R] R badly lags matlab on performance?
Duncan Murdoch
murdoch at stats.uwo.ca
Sat Jan 3 23:22:00 CET 2009
On 03/01/2009 1:37 PM, Ajay Shah wrote:
>> As for jit and Ra, that was immediate reaction too but I found that jit does
>> not help on your example. But I concur fully with what Ben said --- use the
>> tool that is appropriate for the task at hand. If your task is running for
>> loops, Matlab does it faster and you have Matlab, well then you should by all
>> means use Matlab.
>
> A good chunk of statistical computation involves loops. We are all
> happy R users. I was surprised to see that we are so far from matlab
> in the crucial dimension of performance.
>
I don't know Matlab, but I think the thing that is slowing R down here
is its generality. When you write
a[i] <- a[i] + 1
in R, it could potentially change the meaning of a, [, <-, and + on each
step through the loop, so R looks them up again each time. I would
guess that's not possible in Matlab, or perhaps Matlab has an optimizer
that can recognize that in the context where the loop is being
evaluated, those changes are known not to happen. It *would* be
possible to write such an optimizer for R, and Luke Tierney's byte code
compiler-in-progress might incorporate such a thing.
For the difference in timing on the vectorized versions, I'd guess that
Matlab uses a better compiler than gcc. It's also likely that R
incorporates some unnecessary testing even in a case like this, because
it's easier to maintain code that is obviously sane than it is to
maintain code that may not be. R has a budget which is likely several
orders of magnitude smaller than Mathworks has, so it makes sense to
target our resources at more important issues than making fast things
run a bit faster.
Duncan Murdoch
More information about the R-help
mailing list