How to make my algorithm work faster
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello everyone! Sorry if this is a problem with a very simple solution but I'm quite new to matlab and to programming. I have an algorithm in which I need merge 2 very big tables. I need to do this merge 3 times and each time is a little bit different. I made 3 scripts for each merge with a while in which I compare the value on the first column in both tables and i need to add the lower one plus some values in the new table. This takes a lot and i would be really thankful if someone could help me make it work faster. This is my first question on this forum and any tips about how to create a question would be helpful as well.
6 comentarios
Stephen23
el 23 de Ag. de 2017
" I only worked in c++ and a little bit in python and i'm having some issues getting used to Matlab"
Forget everything you know about C++ and Python: they work in totally different ways to MATLAB.
The introductory tutorials are the recommended way to learn important MATLAB concepts:
Respuestas (2)
Jan
el 23 de Ag. de 2017
Editada: Jan
el 23 de Ag. de 2017
Addressing the field of the table costs time. Because you read only in .Var1 in both tables, you can use a temporary variable efficiently:
T1 = Table1.Var1;
T2 = Table2.Var1;
The term "Table" occurs very frequently in the code such that is looks rather redundant. The naming of variables is a question of taste, but everything which improves the readability might be an advantage for understanding the code. Sometimes a patterns in the code get clear with a better readability. I prefer "k1" instead of "kTable1".
Replace:
kTabel1 = 1;
kTabel2 = 1;
if Tabel1.Var1(1)>Tabel2.Var1(1)
while Tabel1.Var1(kTabel1)>Tabel2.Var1(kTabel2)
kTabel2=kTabel2+1;
end
kTabel1=kTabel1+1;
end
by:
k1 = 1;
k2 = 1;
if T1(1) > T2(1)
k2 = find(T1(1) > T2, 1);
k1 = 2;
end
In opposite to the first version of your code, Table3 is not pre-allocated before the loop in the last version. This is slow down the processing substantially. The iterative growing of arrays requires an exponentially growing amount of resources. This was better - except for the name:
Tabel3 = zeros(hTabel1+hTabel2+5, 3);
I have only a few experiences with working in tables. I guess the creation of a double matrix is faster. Then you can create the table after the loop in one step.
"cell" is an important builtin function. Shadowing it by a local variable is not an error, but confusing.
cell = {time, Tabel1.Var3(kTabel1-1)*Tabel2.Var3(kTabel2-1), ...
Tabel1.Var5(kTabel1-1)*Tabel2.Var5(kTabel2-1)};
Tabel3(kTabel3,:)=cell;
Or I assume this is faster:
Tabel3(k3, 1) = time;
Tabel3(k3, 2) = T1V3(k1 - 1) * T2V3(k2 - 1);
Tabel3(k3, 3) = T1V5(k1 - 1) * T2V5(k2 - 1);
With "T1V3" was set as shortcut to "Table1.Var3".
Instead of:
ok = false;
if xyz
ok=true;
end
if ~ok && abc
ok=true;
end
if ~ok ...
you can write:
if xyz
...
elseif abc
...
elseif ...
This will not reduce the runtime a lot, but it is nicer to read.
2 comentarios
Jan
el 23 de Ag. de 2017
I guess I cannot do that.
Cannot do what? A pre-allocation is essential.
Note that it is much easier to improve your code, when we can run it. So provide some representative inputs.
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!