Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Strcat speed

Subject: Strcat speed

From: Kurt

Date: 1 Oct, 2010 09:14:05

Message: 1 of 6

I used the following code to load a number of .mat files:

for i = 1:aircraft
    iname = num2str(i);
    filename = strcat('C:\route_cost_ac',iname,'.mat');
    load(filename);
    %wall of other code
end

However, using this method the script becomes very slow, according to the profiler due to the strcat command (and in a lesser extent the num2str).

Is there any way I can circumvent the strcat command?

Thanks

Subject: Strcat speed

From: Ross W

Date: 1 Oct, 2010 09:34:05

Message: 2 of 6

"Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message <i848ot$g7b$1@fred.mathworks.com>...
> I used the following code to load a number of .mat files:
>
> for i = 1:aircraft
> iname = num2str(i);
> filename = strcat('C:\route_cost_ac',iname,'.mat');
> load(filename);
> %wall of other code
> end
>
> However, using this method the script becomes very slow, according to the profiler due to the strcat command (and in a lesser extent the num2str).
>
> Is there any way I can circumvent the strcat command?
>
> Thanks


     filename = ['C:\route_cost_ac' iname '.mat'];

(not tested)

Ross

Subject: Strcat speed

From: Kurt

Date: 1 Oct, 2010 09:56:05

Message: 3 of 6

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i849ud$22b$1@fred.mathworks.com>...
> "Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message <i848ot$g7b$1@fred.mathworks.com>...
> > I used the following code to load a number of .mat files:
> >
> > for i = 1:aircraft
> > iname = num2str(i);
> > filename = strcat('C:\route_cost_ac',iname,'.mat');
> > load(filename);
> > %wall of other code
> > end
> >
> > However, using this method the script becomes very slow, according to the profiler due to the strcat command (and in a lesser extent the num2str).
> >
> > Is there any way I can circumvent the strcat command?
> >
> > Thanks
>
>
> filename = ['C:\route_cost_ac' iname '.mat'];
>
> (not tested)
>
> Ross

This works ok, but then the problem shifts to the num2str (or sprintf as I'm now using). Any way to speed up these or use other commands them?

iname = sprintf('%d',i) or
iname = num2str(i)

Subject: Strcat speed

From: Ross W

Date: 1 Oct, 2010 10:15:32

Message: 4 of 6

"Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message <i84b7l$nqm$1@fred.mathworks.com>...
> "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i849ud$22b$1@fred.mathworks.com>...
> > "Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message <i848ot$g7b$1@fred.mathworks.com>...
> > > I used the following code to load a number of .mat files:
> > >
> > > for i = 1:aircraft
> > > iname = num2str(i);
> > > filename = strcat('C:\route_cost_ac',iname,'.mat');
> > > load(filename);
> > > %wall of other code
> > > end
> > >
> > > However, using this method the script becomes very slow, according to the profiler due to the strcat command (and in a lesser extent the num2str).
> > >
> > > Is there any way I can circumvent the strcat command?
> > >
> > > Thanks
> >
> >
> > filename = ['C:\route_cost_ac' iname '.mat'];
> >
> > (not tested)
> >
> > Ross
>
> This works ok, but then the problem shifts to the num2str (or sprintf as I'm now using). Any way to speed up these or use other commands them?
>
> iname = sprintf('%d',i) or
> iname = num2str(i)

Yes num2str is slow because it is very general.
I doubt that's there's anything much faster than sprintf

I am very surprised that sprintf could be slower than the load and the "wall of other code", but have to take your word for it that you are reading the profiler output correctly.

Ross

Subject: Strcat speed

From: Yair Altman

Date: 1 Oct, 2010 14:54:05

Message: 5 of 6

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i84cc4$76f$1@fred.mathworks.com>...
> "Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message <i84b7l$nqm$1@fred.mathworks.com>...
> > "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i849ud$22b$1@fred.mathworks.com>...
> > > "Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message <i848ot$g7b$1@fred.mathworks.com>...
> > > > I used the following code to load a number of .mat files:
> > > >
> > > > for i = 1:aircraft
> > > > iname = num2str(i);
> > > > filename = strcat('C:\route_cost_ac',iname,'.mat');
> > > > load(filename);
> > > > %wall of other code
> > > > end
> > > >
> > > > However, using this method the script becomes very slow, according to the profiler due to the strcat command (and in a lesser extent the num2str).
> > > >
> > > > Is there any way I can circumvent the strcat command?
> > > >
> > > > Thanks
> > >
> > >
> > > filename = ['C:\route_cost_ac' iname '.mat'];
> > >
> > > (not tested)
> > >
> > > Ross
> >
> > This works ok, but then the problem shifts to the num2str (or sprintf as I'm now using). Any way to speed up these or use other commands them?
> >
> > iname = sprintf('%d',i) or
> > iname = num2str(i)
>
> Yes num2str is slow because it is very general.
> I doubt that's there's anything much faster than sprintf
>
> I am very surprised that sprintf could be slower than the load and the "wall of other code", but have to take your word for it that you are reading the profiler output correctly.
>
> Ross


In your case, num2str uses int2str (which is thus faster) which in turn uses sprintf (which is thus even faster). It is therefore impossible for sprintf to be *slower* than num2str.

You can speed up (and simplify) your code by combining the sprintf and the strcat as follows:
filename = sprintf('C:\\route_cost_ac%d.mat',iname); % note the '\\'

However - this is only an academic discussion since whichever method you choose it will always (*ALWAYS!*) be much faster than the file-load() operation that it serves. So don't waste your time fixing the sprintf/num2str/strcat but rather the load() operation.

Note: you may possibly see in the profiler report that much time is spent on the strcat line rather than the load() line - this is merely a profiler artifact (due to internal optimizations) and does NOT reflect the actual time spent in the load() function. To see that this is indeed so, place a tic function immediately before the load() and a toc immediately after it and compare the output to a similar construction surrounding the strcat() line.

Yair Altman
http://UndocumentedMatlab.com

Subject: Strcat speed

From: Jan Simon

Date: 1 Oct, 2010 16:18:06

Message: 6 of 6

Dear Kurt,

> for i = 1:aircraft
> iname = num2str(i);
> filename = strcat('C:\route_cost_ac',iname,'.mat');
> load(filename);
> %wall of other code
> end
>
> However, using this method the script becomes very slow, according to the profiler due to the strcat command (and in a lesser extent the num2str).

No. STRCAT and NUM2STR are not the problem. I did not ask my crystal ball, but the output of the profiler has a limited validity if the JIT-accelerator reorders the commands in a function.

LOAD without catching the output in a variable floods the workspace with variables. The dynamically added variables fill the internal lookup-table of pointers to names and data of variables. Therefore the FOR loop gets slower and slower. And even NUM2STR and STRCAT can get slower, but this is not caused by the functions, but by the polluted lookup-tables.

Please try to catch the loaded data in a struct:
  S = load(filename);
Now the new variables are separated from the workspace.

Btw.: What happens, if the loaded data contain a variable called "strcat", e.g. a function handle... Can you guess it? Can you imagine, that Matlab needs intelligent (== slow) methods to catch this?

Kind regards, Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us