Performance optimization (allocation inside a for loop)
Rob Mahurin
rob at utk.edu
Thu Apr 2 12:21:26 CDT 2009
On Apr 2, 2009, at 12:23 PM, John W. Eaton wrote:
> | > Rob Mahurin wrote:
> | > > octave:29> tic; n = 1e5; retval = 1:n; toc
> | > > Elapsed time is 0.000756025 seconds.
> | > > octave:30> tic; n = 1e5; retval = (1:n)(1:n); toc
> | > > Elapsed time is 0.00757694 seconds.
> | > > octave:31> tic; n = 1e5; retval = [1:n]; toc
> | > > Elapsed time is 0.0125589 seconds.
> | >
> | > Can someone explain the difference between "1:n" and "[1:n]"?
> I would
> | > normally write the brackets because I find that way clearer;
> does this
> | > mean "1:n" is actually the "preferred" way?
>
> In Octave, an expression like 1:n creates a range object, which
> contains only the base, limit, and increment as double precision
> values, so no matter how many elements are in the range, it only takes
> a few bytes of storage (24 for the data plus some overhead for the
> internal octave_value object itself).
Thanks for the explanation, John.
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.
Cheers,
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