Performance optimization (allocation inside a for loop)
Rob Mahurin
rob at utk.edu
Thu Apr 2 13:10:43 CDT 2009
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
--
Rob Mahurin
Department of Physics and Astronomy
University of Tennessee 865 207 2594
Knoxville, TN 37996 rob at utk.edu
More information about the Help-octave
mailing list