sprintf command anormally slow

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

The code in your quesiton is not set up to time that section. I see two toc() rather than
tt = tic();
% code
tt2 = toc(tt);
Maybe you're subtracting the 2nd toc() from some other reference time but that's not in the code that we see.
Aside from that, some obvious follow-up questions would be
  • If you move the data to your local machine and run the same code, is it noticeably faster?
  • What does one of those variable (ie, tbl4) look like? You mentioned they are text lines. Does that mean they are char arrays, strings, and how large? (I doubt this would be a bottleneck but it's worth checking)
  • Are you getting any warnings or errors within this section?
Etienne Droz's answer moved here as a comment.
Hi,
Many thanks for your answer !
Yes I was substracting tt2 to tt to get the time of this fraction of code. Sorry it wasn't clear.
The variables are char arrays. They look like:
tbl4= ['\includegraphics[width=\linewidth]{images/timeplot',num2str((measure-1)*nbsensor+sensor+1) ,'.jpg}'];
I have handled the warnings. I don't have them anymore.
I will try to run everything from the local machine. But at the end of the day I would like all my colleagues to have an easy access to the code so it should be on the network. Anyways, I will let you knowonce I manage to run it all on the desktop.
Thanks again !
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.
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

Products

Asked:

on 22 Nov 2019

Commented:

on 25 Nov 2019

Community Treasure Hunt

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

Start Hunting!