What is the difference between the effect of clear and clearvars?
200 views (last 30 days)
Shashank Prasanna on 29 Jan 2013
clearvars just clears variables you specify, in the way you specify.
clear is more powerful in the sense you can clear variables, variables, functions, compiled scripts, MEX-functions
Check our the doc for more info:
More Answers (3)
Jab re on 19 Jan 2017
if you define a global variable, clearvars will remove it as well, however, somehow its value is still accessible in the memory and next time you define a global variable with the same name, the value of the previously removed variable will be assigned to the new one! Which is very strange!
Test = 10;
if you check the workspace after above, you will see there is no variable called Test at this point, however:
Now Test has a value of 10 again! This strange behavior does not happen if you use clear all to remove the variables.
I am not sure if it is a bug or something designed, but clearvars did cause me problems and I found it the hard way!
Walter Roberson on 30 Jul 2021
@Kien Pham asked above whether there was a timing difference between clear and clearvars.
The answer astonished me: there is a big timing difference between the two!!
In the below code, I generate random assignment statements and write them to files. The files differ only in function name, and in whether they use "clear" or "clearvars" or have no clearing (keeping in mind that local variables are supposedly cleared when a function returns, so using a function is another form of clearing.)
Because the exact same assignment statements are used, there will be differences between the variations due to differences in how long the clearing takes; there should also be normal execution variances, so if the values are close then it is worth testing repeatedly to see if any particular relative order is accidental.
Note that it would normally be expected by programmers that the great majority of the execution time would be due to the assignment statements. Programmers tend to expect that clearing variables is fast. Mathworks does however warn that if you have a loop with a local variable that is being overwritten, then clearing the local variable is not good for performance
The results are:
- not clearing is fastest
- using "clear" is about 14 times slower -- too much difference for it to be due to chance
- using "clearvars" is much slower. In this test, 25000 times slower!! And I could tell from my tests that the time taken is proportional to roughly the square of the number of variables !!
When I analyze the code for clearvars, I can see that in the case that no argument is provided, that it would execute
evalin('caller', 'clear -regexp ^.')
which tells us that the problem isn't exactly with clearvars, but rather that the speed problem is when using clear with -regexp .
The poor performance in this case is astounding, and I will create a support case about it.
N = 12000;
tn = tempname;
tn1 = tn + "clear.m";
tn2 = tn + "clearvars.m";
tn3 = tn + "noclear.m";
[folder, fn1, ext] = fileparts(tn1);
[~, fn2, ~] = fileparts(tn2);
[~, fn3, ~] = fileparts(tn3);
[fid1, msg] = fopen(tn1, 'w');
if fid1 < 0; error('Failed to open file "%s" because "%s"', tn1, msg); end
cleanme1 = onCleanup(@()delete(tn1));
[fid2, msg] = fopen(tn2, 'w');
if fid2 < 0; error('Failed to open file "%s" because "%s"', tn2, msg); end
cleanme2 = onCleanup(@()delete(tn2));
[fid3, msg] = fopen(tn3, 'w');
if fid3 < 0; error('Failed to open file "%s" because "%s"', tn3, msg); end
cleanme3 = onCleanup(@()delete(tn3));
fprintf(fid1, "function %s\n", fn1);
fprintf(fid2, "function %s\n", fn2);
fprintf(fid3, "function %s\n", fn3);
AL = 'A':'Z';
rn = AL(randi(length(AL), N, 62));
rv = compose(" = %.15g;", rand(N,1));
rl = rn + rv;
fprintf(fid1, "%s\n", rl);
fprintf(fid2, "%s\n", rl);
fprintf(fid3, "%s\n", rl);
fclose(fid1); fclose(fid2); fclose(fid3);
clear(fn1); clear(fn2); clear(fn3); %needed because m files were modified
fh1 = str2func(fn1);
fh2 = str2func(fn2);
fh3 = str2func(fn3);
fprintf('timing with clear\n');
fprintf('timing with clearvars\n');
fprintf('timing with no clear or clearvars\n');
Fady Dawoud on 28 Sep 2021
Edited: Fady Dawoud on 28 Sep 2021
I also would like to know the answer perhpas from a mathwork developer privy to the underlying code differences. I am encountering a scenario where if I use 'clearvars' from within a running script, memory is not released (as seen on task manager) but when I stop script and run 'clearvars' manually (using F9), magically memory is released. I tried the same with 'clear all' and in both cases (within running script vs manually) memory is released.
I know there are other ways to re-write code to be more memory efficient (most obviously using functions instead of scripts) but it would be helpful to know the difference in mechanics between 'clear all' and 'clearvars'.