How can i speed up my code?
2 views (last 30 days)
Show older comments
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
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.
Accepted Answer
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
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!