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.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…