MATLAB Answers

How can i speed up my code?

1 view (last 30 days)
Benoit Chemin
Benoit Chemin on 26 Mar 2020
Commented: Benoit Chemin on 26 Mar 2020
Hello. I have the following code in a loop of 8760 elements. It takes almost all the time of my algorithm. I wanted to know if it is possible to improve it and to make it take less time:
Here, Conso is a number, for example 50. ToutesPlages is a 1x80000 structure with the field MinConso and MaxConso and its length .

  6 Comments

Show 3 older comments
Benoit Chemin
Benoit Chemin on 26 Mar 2020
Ok so the content of the structure ToutesPlages is like that:
In my code his length is around 80000 but let's say 20 for the example
i want for a given value of Conso to get all the the rows that respect; MinConso<Conso<MaxConso
The following code works but i wanted to know if it can be improved
Conso=2*rand;
for i=1:10
% function [ConfigurationsPossibles] = Configurations_possibles(Conso,ToutesPlages)
Indices=([ToutesPlages.MinConso]<Conso)&[ToutesPlages.MaxConso]>Conso;
ConfigurationsPossibles=ToutesPlages(Indices);
% Operations with ConfigurationsPossibles
end
% end
dpb
dpb on 26 Mar 2020
The indexing operaton appears optimal in isolation; still nothing in code snippet as shown that indicates it needs to be in the loop, however.
But, presuming avoid executing the same code multiple times with the same data, unless can somehow create the data in the array to include only the wanted at that time so don't have to then discard the unwanted, don't see much else to be done here.
That shouldn't be a major bottleneck operation, anyway, wouldn't think unless using the struct array is causing a lot of overhead in addressing behind the scenes.
As always, don't try to peephole optimize; if code performance isn't acceptable, profile the whole thing first so know are working on what is the actual problem area.
Benoit Chemin
Benoit Chemin on 26 Mar 2020
Thank you for your answer. I don't think i can put the code outside the loop because i need for every value of Conso ( which is different each time) the structure ConfigurationsPossibles and then to do some operations with it. This indexing seems to be the fasted method that i can do to get the wanted element of a given structure.
I have done a profile of the whole code but this fonction is what takes the most time. So i tried to improve this.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 26 Mar 2020
Edited: Matt J on 26 Mar 2020
Please do not provide code in the form of embedded images. It prevents us from conveniently copy/pasting relevant sections.
In any case, the concatenations [ToutesPlages.MinConso] and [ToutesPlages.MaxConso] are expensive operations, unnecessarily repeated throughout the loop. Since they are not changing, you should create dedicated arrays for them prior to the loop.
LowerBounds=[ToutesPlages.MinConso];
UpperBounds=[ToutesPlages.MaxConso];
for i=1:N
Conso=...
ConfigurationsPossibles=ToutesPlages( LowerBounds<Conso & Conso<UpperBounds);
end

  1 Comment

Benoit Chemin
Benoit Chemin on 26 Mar 2020
Works very good. Thank you so much!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by