
7 Tuning and Managing M-Files
Message — Code (Original Line Numbers)
Explanation and Updated Code (New
Line Numbers)
24: 'notline' might be growing inside
a loop. Consider preallocating for
speed.
—————————————————
22 nothandle = ~ishandle(hline);
23 for nh = 1:numel(hline)
24 notline(nh) = ~ishandle(hline(nh))
...
When you increase the s ize of an array within
a loop, it is inefficient. Before the loop,
preallocate the array to its maximum size to
improve p erformance. For more information,
see “Preallocating Memory” in the M ATLAB
Programming documentation. In the example,
addanewlinetopreallocate
notline before
the loop.
23 notline = false(size(hline));
24 for nh = 1:numel(hline)
25 notline(nh) = nothandle(nh) ...
24: Use STRCMPI(str1,str2) instead of
using LOWER in a call to STRCMP.
—————————————————
24 notline(nh)=~ishandle(hline(nh)) ||
~strcmp('line',lower(get(hline(nh),
'type')));
While
strcmp
('line',lower(get(hline(nh)'type'))
converts the result of the get function to a
lowercase string before doin g the comparison,
the
strcmpi function ignores the case while
performing the comparison, with advant ag es
thatincludemoreefficiency.Changeline25to
notline(nh) = nothandle(nh) ||
~strcmpi('line',get(hline(nh),'type'));
28: NUMEL(x) is usually faster than
PROD(SIZE(x)).
—————————————————
28 for nl = 1:prod(size(hline))
See the same message and explanation
reported for line 23. Change the line 29 to
for nl = 1:numel(hline)
7-22
Commenti su questo manuale