How can i speed up my code?

2 次查看(过去 30 天)
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 个评论
dpb
dpb 2020-3-26
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 2020-3-26
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.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2020-3-26
编辑:Matt J 2020-3-26
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by