sprintf command anormally slow

조회 수: 3 (최근 30일)
Etienne Droz
Etienne Droz 2019년 11월 22일
댓글: Adam Danz 2019년 11월 25일
Hello,
I am runnig a code that reads a .tex layout, sligthly modifies it and saves it.
The latex layout is in a cell of 197 lines and I am modiying only about 20 lines nevertheless the code is for some reasons super slow.
for example this part lasts about 10 seconds:
tt=toc
A{ind+1} = sprintf('%s',tbl1);
A{ind2+1} = sprintf('%s',tbl2);
A{ind3+1} = sprintf('%s',tbl3);
A{ind4+1} = sprintf('%s',tbl4);
A{ind5+1} = sprintf('%s',tbl5);
A{ind6+1} = sprintf('%s',tbl6);
A{ind12+1} = sprintf('%s',tbl7);
A{ind13+1} = sprintf('%s',tbl8);
A{ind14+1} = sprintf('%s',tbl9);
% A{ind15+1} = sprintf('%s',tbl10);
A{ind16+1} = sprintf('%s',tete);
A{ind17+1} = sprintf('%s',chan);
%final table
A{ind18+1} = sprintf('%s',tbl20);
A{ind18+2} = sprintf('%s',tbl21);
A{ind18+3} = sprintf('%s',tbl22);
A{ind18+4} = sprintf('%s',tbl23);
A{ind19+1} = sprintf('%s',tete2);
save(deb);
tt2=toc
Where A is the cell array that contains the .tex layout. ind1->ind19 are the indices of the lines that should be modified and tbl1->tbl23 are the new text lines to write in the cell array A.
Anyone knows how I can speed that up ? I am running on matlab 2018a. Note that my working directory is not on the computer hard drive but on a server. I think this would slow down the writing process but for that specific part it shouldn't have an influence right ?
Thanks in advance !
  댓글 수: 4
Guillaume
Guillaume 2019년 11월 25일
Ughh! numbered variables!
for idx = 1:10
A{ind+idx} = sprintf('%s', tbl{idx});
end
does the same as your 10 lines of code and is much easier to extend to more indices, as long as you use indexing instead of numbered variables.
More to the point, why are you even doing
sprintf('%s', somecharvector)
? It just returns the same char vector.
Adam Danz
Adam Danz 2019년 11월 25일
Questions about sprintf
Is the sprintf command section the only section of your code that is slow? I would find it very odd if sprintf() commands are the bottleneck. I have two comments on that.
First, if this is what' your're sending into the sprintf() function, I doubt you need sprintf() at all.
['\includegraphics[width=\linewidth]{images/timeplot',num2str((measure-1)*nbsensor+sensor+1) ,'.jpg}']
That line produces a char array so why do you need sprintf()?
Second, please execute this line below. Let's make sure you're using Matlab's sprintf() instead of some custom function of the same name that is overshadowing the built-in function.
which sprintf -all
Accessing a remove network
I often use a remote network and it's almost always slower than working on a local machine but as you menionted, a shared network is common in the workplace. If the network connect or the machine hosting Matlab were the bottlenecks, I would expect all of your code to be slower than when you run it on a local machine -- not just the sprintf section.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by