Performance optimization (allocation inside a for loop)
Jaroslav Hajek
highegg at gmail.com
Thu Apr 2 13:39:04 CDT 2009
On Thu, Apr 2, 2009 at 8:10 PM, Rob Mahurin <rob at utk.edu> 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
More information about the Help-octave
mailing list