Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

matlab - Increase the performance by removing CLEAR ALL

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...