help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Performance optimization (allocation inside a for loop)


From: Jaroslav Hajek
Subject: Re: Performance optimization (allocation inside a for loop)
Date: Thu, 2 Apr 2009 20:39:04 +0200

On Thu, Apr 2, 2009 at 8:10 PM, Rob Mahurin <address@hidden> wrote:
> On Apr 2, 2009, at 1:25 PM, John W. Eaton wrote:
>> On  2-Apr-2009, Rob Mahurin wrote:
>>
>> | Any idea why (1:n)(1:n) makes a full matrix more quickly than [1:n]?
>> | Just looking at timing, it looks like [] waits for something that ()
>> | () doesn't.  I would have expected any difference to go the other
>> way.
>>
>> What do you mean by "more quickly?"
>
>
> Well, if I do
>
> lim = 1e3;
> n = 1e5;
> for i = lim:-1:1; tic;
>        retval = (1:n)(1:n); t_paren(i) = toc; endfor
> for i = lim:-1:1; tic;
>        retval = [1:n]; t_brack(i) = toc; endfor
> plot([t_paren; t_brack]')
>
> then t_paren is mostly around 7 milliseconds, but t_brack is mostly
> around 10.5 milliseconds.  With n = 1e6 the times are 70 ms and 100
> ms or so.
>
> Rob
>

This is caused by the underlying implementation - when concatenated,
the range is first converted to array, then inserted into a result
array, thus a copy is made twice.

The indexing expression, OTOH, only converts the range to matrix, but
does not do any further copying.


-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]