MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by C Zeng
on 13 Jun 2013

Hi, All,

I am doing a large scale computation problem, and I have several variables, which is large scale (>100MB). I was told that MATLAB will not automatically remove the not used memory in running, is that right?

If so, can I put into the code that "clear -var" to remove it, so that speedup the program? Is there some one who has done this before? What is the effect then?

Thanks.

*No products are associated with this question.*

Answer by Walter Roberson
on 14 Jun 2013

Accepted answer

There are some situations in which removing unused large variables could speed up calculations a lot, but if the variables are just sitting in memory without being touched then *probably* it is not significantly affecting the speed of your calculations.

What hammers speed the most in MATLAB tends to be failing to pre-allocate arrays to reasonable sizes, and then to increase the size of the array on (most or every) iteration.

Show 3 older comments

C Zeng
on 14 Jun 2013

Walter, thanks for sharing this.

Yes, I agree that memory read/write is much faster than hard drive. And if all variables are stored in memory, then removing unused variables may not have any advantage.

Just in case, memory is not enough and system calls for hard drive, then swapping is time consuming!

Regarding to your second point, yes there may be chance that removing inactive big variables can help, as you said. Also I found that C++ runs faster than MATLAB generally, especially for lots of loops. Could you explain it? I was told that MATLAB allocates memory different than C++. Is that the reason?

I really appreciate it. Nice weekend!

Walter Roberson
on 15 Jun 2013

C++ is compiled to machine language. MATLAB is an interpreter. MATLAB Compiler, despite its name, produces an intermediate form that is interpreted. MATLAB Coder processed code *is* compiled, but MATLAB Coder can only handle a fraction of MATLAB and libraries.

Loops have gotten much better in time in MATLAB.

MATLAB knows how to interface to numeric routines such as BLAS that are highly optimized and potentially multiprocessor (even without the Parallel Computing Toolkit); MATLAB also has some heuristics to decide when it would be faster *not* to use those numeric libraries. MATLAB is thus *potentially* quite efficient, depending on what is to be done.

You could write calls to those routines in C++, but it would probably take more work and be less clear. You get a trade-off of development time versus execution time. With fast systems these days, development time is often (but not always) the more expensive over the life-cycle of the program

C Zeng
on 17 Jun 2013

Dear Walter, thanks for your comments, I did not mean to hurt Matlab, in stead I like it. I was told that C++ is more faster for large scale problem than MATLAB, and it may be so however the advantage may not be too much.

And yes you are right, the development time is much less in Matlab than C++ and that is why I prefer Matlab.

For the loop, I recently found vectorize the code can largely increase the speed in Matlab. I have not tried parallel toolkit but will do it later. Thanks for your answers!

## 1 Comment

## C Zeng

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/78979#comment_155034

Sorry, I may not be very clear on this question. I am doing large scale optimization computation. I am wondering if I remove some unused large variable to make memory efficient, will there a chance to speedup the computing a lot?

Or does someone have done this before? Thanks.