sprintf command anormally slow

3 views (last 30 days)
Etienne Droz
Etienne Droz on 22 Nov 2019
Commented: Adam Danz on 25 Nov 2019
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 Comments
Guillaume
Guillaume on 25 Nov 2019
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 on 25 Nov 2019
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.

Sign in to comment.

Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!