I need to make my program faster

Hi!
I'm new with Matlab but I know about C. I have really serious problems with the execution time, because I have to execute the function thousand times.
I don't have idea of the sizes of the vectors in the loops, it depends of the entry of the main function (unpredictable). In my code:
r=1;
n(1)=0;
Tramo_n(1)=Tramo_conteo(1);
for i=2:length(Tramo_conteo)
if (strcmp(Tramo_conteo(i-1),Tramo_conteo(i))==1)
n(r)=n(r)+Numero_casos(i-1);
Fecha_inicio(r)=Fecha_conteo(i-1);
Fecha_final(r)=Fecha_conteo(i-1);
for k=1:length(Tramos)
if strcmp(Tramos(k),Tramo_conteo(i))==1
Tiempo_limite(r)=2*Plazas_tot(k);
if n(r)>Plazas_tot(k)
Fecha_inicio(r)=Fecha_conteo(i-1);
Valido_plazas(r)=1;
dif=n(r)-Plazas_tot(k);
n(r)=Plazas_tot(k);
q(r)=1;
Tramo_n(r)=Tramo_conteo(i-1);
r=r+1;
n(r)=dif;
Tramo_n(r)=Tramo_conteo(i);
else
q(r)=0;
if ((Plazas_tot(k)-n(r))<round(Plazas_tot(k)/2))
Valido_plazas(r)=1;
else
Valido_plazas(r)=0;
end
end
end
end
else
Fecha_inicio(r)=Fecha_conteo(i-1);
Fecha_final(r)=Fecha_conteo(i-1);
Tramo_n(r)=Tramo_conteo(i-1);
q(r)=0;
r=r+1;
Tramo_n(r)=Tramo_conteo(i);
n(r)=0;
end
end
Tramo_n(end)=Tramo_conteo(end);
n(end)=n(end)+Numero_casos(end);
for k=1:length(Tramos)
if strcmp(Tramos(k),Tramo_conteo(end))==1
if n(end)>Plazas_tot(k)
dif=n(end)-Plazas_tot(k);
n(end)=Plazas_tot(k);
q(end)=1;
Tramo_n(end+1)=Tramo_conteo(end);
n(end+1)=dif;
q(end+1)=0;
else
q(end)=0;
end
end
end
It takes oves 18 seconds. I really need to improve it, as well as the next code:
for i=1:length(Plazas_conteo) %NÚMERO DE PLAZAS EN LA MISMA SITUACIÓN DENTRO DEL CONTEO (ES UNA CORRECCIÓN DE LOS DATOS DE ENTRADA
aux=strsplit(Plazas_conteo(i),'-');
if length(aux)>1
Numero_casos(i)=abs(double(aux(1))-double(aux(2)))+1;
end
end
It takes over 5 seconds, and the reading of files (20k lines aprox.):
tabla_conteos=readtable('Situaciones detectadas.xlsx');
Tipo_conteo=string(tabla_conteos{:,6});
Plazas_conteo=string(tabla_conteos{:,4});
Tramo_conteo=string(tabla_conteos{:,2});
Fecha_conteo=string(tabla_conteos{:,7});
Also takes 4 seconds.
Thanks so much for helping me!!

4 Comments

Jan
Jan on 28 Nov 2018
Edited: Jan on 28 Nov 2018
The sizes of the inputs are really unpredictable? Can it be a vector of length 10^9? Please mention at least the type of the input, preferably some example data. Without the possibility to run the code, it is hard to guess, where improvements are useful.
If reading the file is the bottleneck, most likely a faster network or harddisk (SSD!) will help.
It looks like the outputs are not pre-allocated, but growing iteratively. This is a bad idea in general. Dur to the lack of comments, it is hard to find out, what the code should do, so recommending suitable a pre-allocation is nearly impossible. Add useful comments to all of your codes.
What is the type, size and contents of Plazas_conteo ? Maybe strtok is much faster or the complete loop can be replaced. Without knowing the inputs, too much guessing is required for an improvement of the code. Please edit the question and add more details.
In addition to what Jan has said (which seems right) you want to try using the profile utility in MATLAB. This will tell you explicitly where your bottlenecks lie.
I'm new with Matlab but I know about C.
In that case, you could also combine Matlab with C/C++ via mex routines. But try the above suggestions first before jumping into mex functions.
Matlab strength lies in vector math, so converting a for loop into a matrix operation may provide a substantial speed boost.
I see you are reading a .xlsx file, but working with .csv files may be faster. There is also a xlsread function - not sure if it'll be much fatster than readtable.
If you can use csv files instead, consider using more primitive functions with less data-checking overhead, such as textscan, fscanf, csvread, dlmread, etc.
Thank you all!
I'm glad to anounce you that I have split all the main code in different functions and I only execute the necesary ones each time, so I reach (excepting the first run) times of 2 seconds!
Of course I will mind all your advices and keep improving my code.
One more time, thank you.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!