Programming long script gets extremely slow

Hi, im currently writing a script in R2016a. It is 18k lines long right now, and it's done for about 25%. However, the programming gets really slow right now (it did since about 12-15k lines). The running of the script goes just fine, but when I try to edit it it severely lags: whatever I type into the code takes several seconds to actually appear. Does anyone else have similar experience with this issue and an idea on how to fix it? Thanks a lot!

3 Comments

I'm really curious about what on earth you could be writing that would be 72k lines long.
In general, it is good for code performance to split your functions over several files. That also improves a modular method of coding, as it will be easier to improve performance of those smaller sub-units.
Is there a compelling reason why your code has to be in a single file? Otherwise the solution seems pretty obvious to me.
72000 lines of code in one file... ouch! That sounds impossible to write correctly, and impossible to debug. Why not just use the simple methods that everyone else uses: split code into functions, write OO code, make sure that is is vectorized and follows the other good MATLAB practices... ?
I'm highly uncomfortable with one of my GUIDE m-files that is 1320 lines long and that even includes lots of lines of automatically created stuff. I can't imagine writing even a function tens of thousands of lines long, let alone a script!

Sign in to comment.

 Accepted Answer

Jan
Jan on 18 May 2017
Of course writing such a huge M-file in Matlab's editor is slow. It is expected to be. Note that the editor performs a syntax check after all changes, can highlight code blocks, variables, checks if opening and closing brace, brackets or parenthesis have to be highlighted, checks the indentation of for and while loops and let MLint check the validity and efficiency of the code.
Functions or even worse scripts with more than 1000 lines of code are hard to read and to maintain. It gets horrible to debug this code and you cannot apply unit tests to parts of it. Therefore the complexitiy of the code will be such high, that an exhaustive testing is impossible. In consequence no serious programmer would use such code, but delete it without any further investigations.
Split the code to subfunctions. This will improve the editing, maintenance and perhaps even the runtime.

4 Comments

When it comes to code complexity, one rule of thumb is:
"One of McCabe's original applications was to limit the complexity of routines during program development; he recommended that programmers should count the complexity of the modules they are developing, and split them into smaller modules whenever the cyclomatic complexity of the module exceeded 10."
You can see the McCabe complexity of code using the checkcode function with the '-cyc' option. Not all MathWorks functions have complexity 10 or lower, but we try to keep their complexity fairly low. I am curious (from a morbid, rubbernecking at an accident sense) how high the complexity of that 18k line script is (or even if checkcode can handle processing so large a script in a reasonable time.)
A script created by
for k = 1:1000; fprintf('var%04x = rand;\n', k); end
can be huge, have a cyclomatic complexity of 1 and is impossible to debug. If the OP has written 18k lines of code and knows already, that this is about 25% of the total code, it looks like the code is created automatically. But then it would not matter that the editing is slow.
@Elvis: Another idea: edit the file in Notepad++. Then you do not have the comfort of code checks, but the editing works fluently.
@Steven Lord Sorry for the late reply, i just ran the code. Result: The McCabe complexity is 3519. Guess I'll have to split my code into 1400 subscripts? :p I think I will go with @Jan Simon's suggestion and edit it in Notepad++. Thanks all!
How can you possibly even understand code that long. Being able to edit it seems like rearranging deckchairs on the Titanic compared to the problem of maintaining it as working code.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 17 May 2017

Commented:

on 1 Jun 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!