I did the profiling, and this line takes the most of time: running 28449740 times and takes 163.84s.
I am wonderring if there is any version that is faster than min.
[a,b]=min(c(s1,s2));

9 commentaires

KSSV
KSSV le 27 Avr 2018
What is s1 and s2? What exactly you are trying to do? Show us the code.....min is actually very efficient.
The code is as follows. I can not vectorize or use parpool as c here keeps updating within each for loop.
c=1:8760;
d=5123:5146;
tic
for ii=1:41857887
[a,b]=min(c(1,d));
end
toc
Elapsed time is 166.302782 seconds.
min is already optimal. The code is slow because you are running for loop 41857887 time and not changing anything inside. Your code is equivalent even if you remove for. Do this
c=1:8760;
d=5123:5146;
tic
[a,b]=min(c(1,d));
toc
Chaoyang Jiang
Chaoyang Jiang le 27 Avr 2018
Modifié(e) : Chaoyang Jiang le 27 Avr 2018
Yes, I add for-loop here just to show the running time. Actually, in my real code, d and c are not the same and keep updating all the time
KSSV
KSSV le 27 Avr 2018
You replace/ use the updates values......you need not to run a loop.
Ameer Hamza
Ameer Hamza le 27 Avr 2018
@Chaoyang then you will need to show the code, maybe there are some other ways to make it faster. As far is min is concerned it can't be further optimized.
Walter Roberson
Walter Roberson le 27 Avr 2018
If you break out the timing, you will likely find that the cost is in extracting the sub-array c(1,d) to send to the min() function. min() is at worst a linear operation (I say at worst because in theory it could be run in parallel for sufficiently large arrays.)
Stephen23
Stephen23 le 27 Avr 2018
"... this line takes the most of time: running 28449740 times and takes 163.84s."
Sure. But there are several operations on that line, so how do you know that min is the slow one?
Walter Roberson
Walter Roberson le 27 Avr 2018
Reminder: extracting consecutive rows is faster than extracting consecutive columns, so you should consider transposing your array.

Connectez-vous pour commenter.

Réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by