Re: wavread, performance due to memory allocation




"Dan Ash" <dan_ash@xxxxxxxxxxxxx> wrote in message
news:1133421919.590740.280780@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> This may be obvious to most of you, but in case it is not, thought I'd
> save someone some time. Or maybe I am the only idiot out here.
>
> I finally figured out why my scripts incorporating wavread() would
> stall.

*snip code that runs faster with preallocation*

Yes, this technique is known as preallocation. Rather than forcing MATLAB
to allocate more memory for a growing array each time through the FOR loop,
preallocation forces MATLAB to allocate memory once before the FOR loop, and
the FOR loop will fill up the allocated memory. This technique, as well as
a number of others, are described in the "Improving Performance and Memory
Usage" section of the MATLAB documentation:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/ch15_per.html#764852

It's the second technique listed for improving performance. You might also
want to see if the first technique, vectorizing your loop, is possible -- it
might help your code run faster.

Another section in the documentation, "Programming Tips", doesn't deal
directly with performance per se, but it does offer some best practices for
programming in MATLAB, which you may also find useful.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/ch16_pro.html#57435

--
Steve Lord
slord@xxxxxxxxxxxxx


.



Relevant Pages

  • Re: Matlab keeps timing out when I use it to control a webcam
    ... I am using Matlab to control a webcam that takes images ... are nestled in a while loop, so that with each loop of the ... The webcam is a Philips SPC1000NC. ... are getting an even frame rate which would help standardize ...
    (comp.soft-sys.matlab)
  • Re: Mathematica Vs. Matlab
    ... The general DO loop in Macsyma is designed to handle cases where the loop ... Do either Matlab or ... >> number of other software packages. ... >> both Mathematica and MATLAB in the two years since the report was last ...
    (sci.math.symbolic)
  • Re: How to be a good Matlab programmer
    ... was a long way from the loop, and the loop was longer, the reader might ... Possible tips for matlab programmers coming from other languages: ... Also use "keyboard" to interrupt a script ... "whos" is a useful debugging tool. ...
    (comp.soft-sys.matlab)
  • Re: enhance m-files performance by compiling?
    ... as Dan already pointed out: ... Although MATLAB was invented ... of programming skills. ... about 100 lines, and one loop. ...
    (comp.soft-sys.matlab)
  • Re: 100x difference in script execution times
    ... MATLAB now uses just-in-time compilation, ... The P4 trace cache holds something like 12,000 uOps. ... loop fits in this trace cache,, everything runs ... and the P4 can deliver 3 uOps per cycle to the execution units. ...
    (comp.soft-sys.matlab)