18

In Matlab 2014b, when I use CLEAR ALL at the beginning of the script I get the following warning,

For improved performance, consider not using CLEAR ALL within a script

which is not given in the previous releases (As I recall).

The only reason I found is that, when you call a script from outside or from other scripts you do not want to clear the variables in the workspace, and re-generate them every time again and again.

Is there any other reason that I am missing?

How does removing CLEAR ALL improves the performance when using a single script?

NKN
  • 6,482
  • 6
  • 36
  • 55
  • 5
    When using that single script once? Probably nothing. But ``clear all`` doesn't only clear variables, but also loaded functions and mex-functions (and, if i recall correctly, also breakpoints, which are set within that script). Those functions have to be loaded again (which simply costs a bit of time), the breakpoints are simply gone and will make it hard to debug a script, which starts with ``clear all``. To make sure to start with a clean workspace, ``clear variables`` should be sufficient. – Nras Dec 15 '14 at 14:00
  • 6
    I bet you don't get that warning if you use `clear`. The reason is that `clear all` clears functions too, as Nras explains. `clear` only cleans variables. Therefore compiled functions are kept in the memory. Have a [look here](http://stackoverflow.com/questions/16873733/why-does-matlab-run-faster-after-a-script-is-warmed-up) for a bit more information. Amro's answer is especially interesting. =) – Stewie Griffin Dec 15 '14 at 14:06

1 Answers1

16

In R2015b, the semantics of clear were changed. Perhaps in response to the concerns raised in this question, the changes stated in the release notes are:

The clear function no longer clears debugging breakpoints. To clear breakpoints, use dbclear all.

The clear function only clears functions that are not currently running. For example, when you call clear myFun while myFun is running, myFun is not cleared.


This part pertains to pre-R2015b MATLAB versions.

Here's a table of what gets cleared with each input argument.

enter image description here

The table for R2015b is identical except that there is no longer a "Debugging breakpoints" column since they are no longer cleared with clear.

Scripts and functions are cleared, when you can probably just clear variables (red boxes). It does not make a lot of sense to clear the function from memory that is presently being executed. (According to R2015b release notes, this does not happen.)

Also, keeping in mind that scripts execute in the base workspace, you are clearing out all functions that may be used by other scripts. Try looking at the output of inmem after an extended MATLAB tinkering session. You fill find all kinds of MATLAB functions that are loaded into memory for fast access (including 'matlabrc', 'pathdef', and other core scripts that setup your workspace). So, perhaps it's not that it hurts performance of just the script where you call clear all but all other scripts and the interactive command line that is in the base workspace. That would be my guess.

Not performance related, but another reason why a clear all in a script might be a bad idea is that it will clear breakpoints (this can be annoying!), and global+persistent variables. However, it might be the goal to clear global and peristent variables. For global there's clear global, but there's nothing like that for persistent since persistent variables are bound to functions and you would use clear functions or clear whateverFunctionName for them.

Community
  • 1
  • 1
chappjc
  • 30,359
  • 6
  • 75
  • 132