Why/How does MATLAB run faster when the workspace is full of variables?
Show older comments
I've been running some finite element code written for MATLAB that takes up at least 40 variables and near 100 mb of memory in the workspace. This code takes anywhere from 28 to 32 minutes to run.
However, I noticed that if I don't clear the workspace with the CLEAR command and just run the new case, MATLAB runs the code in about 15 to 18 minutes! However, if I do clear the workspace with the CLEAR command and run a new case, it takes about 28 to 32 minutes to finish again!
I'm proficient in MATLAB but not an expert. Can someone enlighten me on why this happens and if there is some "trick" that I can exploit to make MATLAB run even faster?
I am using a Dell Precision T1500 with an i5 (64bit) processor with OS as Windows 7. The version of MATLAB is 7.6.0 (R2008a). Thanks
Accepted Answer
More Answers (2)
Are you using clear or clear all? Matt is right, reading the functions again after a clear all consumes time, but not 15 minutes. Such effects appear only, if a called functions calls clear all repeatedly, e.g. 1000th of times.
If you are using scripts, the uncleared variables can mean a pre-allocation also. Letting a variable grow iteratively consumes a lot of resources. You find a lot of examples for the keyword "pre-allocation" in this forum.
2 Comments
Jared
on 3 Dec 2012
Jan
on 3 Dec 2012
Add an empty line before and after the code, paste it, select it and hit the "{} Code" button. Then only empty lines inside the code are mal-formed, but there is not method to avoid this currently.
While pre-allocation does not matter, if you join a dozen of values only, the general strategy to pre-allocate in general and without any exceptions does not fail, when a part of the program is called unexpectedly for larger datasets also. Usually a serious analysis of all possible inputs takes longer than to insert the standard pre-allocation code.
husain
on 9 Apr 2016
0 votes
dear Matt Fig ,
your really aswesome you had solved my problem also thanking you very much
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!