How can i speed up my code?

2 Ansichten (letzte 30 Tage)
Benoit Chemin
Benoit Chemin am 26 Mär. 2020
Kommentiert: Benoit Chemin am 26 Mär. 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 Kommentare
dpb
dpb am 26 Mär. 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 am 26 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 26 Mär. 2020
Bearbeitet: Matt J am 26 Mär. 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 Kommentar
Benoit Chemin
Benoit Chemin am 26 Mär. 2020
Works very good. Thank you so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by