Line 129 in saveas function is slow!
12 次查看(过去 30 天)
显示 更早的评论
I'm plotting some figures in a script where I create a single figure and then clear the figure and replot to the current axes. I save the figures using saveas after each figure is plotted. Saveas is called 16 times (8 times it saves as a .fig and 8 it saves as a .ai). My function is taking nearly 5 minutes to run with 98.9% of that being saveas line 129 which is this:
if ~isempty(format) && any( exist( ['saveas' format]) == [2 3 5 6] ) %#ok
It doesn't make sense to me that this line should take so long. I'm using Matlab 2007b. Here is the profiler output.
Lines where the most time was spent Line NumberCodeCallsTotal Time% TimeTime Plot 129if ~isempty(format) &&...16 280.099 s98.9% 130feval( ['saveas' format], h, n...81.907 s 0.7% 159print( h, name, ['-d' dev{i}] ...81.121 s 0.4% 136[ops,dev,ext] = printtables; %...80.017 s0.0% 160return80 s0%All other lines 0 s0% Totals 283.145 s100%
Thanks in advance.
Justin
0 个评论
回答(2 个)
Oliver Woodford
2011-8-5
If the question were instead "How can I make saveas faster?", I'd say -
Change line 129 to:
if ~isempty(format) && ~isempty(strmatch(format, char('fig', 'm', 'mfig', 'mmat'), 'exact'))
1 个评论
Fangjun Jiang
2011-8-5
Yes, good idea! The rest of saveas.m did use strmatch( , ,'exact') to compare other format such as .bmp or .eps. This could be a nice enhancement of the saveas() function.
Fangjun Jiang
2011-8-4
It is true for me too. That line consumes more than half of the time if I save the plot to a .fig file and repeat it a dozen times.
You could put it in a good way that the other part of the code is fast.
To understand it further, the time consuming part is the exist() function, it tries to see if a file named 'saveasfig' exists in the MATLAB path. It does and it's a private function. It could also search for saveasfig.mex, saveasfig.p. That is what [2 3 5 6] for.
It sounds unreasonable that it spent 280 seconds on that line of code, unless your computer is really old and slow. I suggest you check your MATLAB path and remove unnecessary folders in it. Run rehash might also help.
>> which saveasfig -all
.\MATLAB\R2007b\toolbox\matlab\general\private\saveasfig.m % Private to general
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!