Dear All,
I found my code spent a lot of cpu time on the following functions:
  1. intersect, took 20.11 seconds, called 232074 times in my code.
My code:
C = intersect(A,B); where A and B are two column arrays.
2. unique, took 8.845 seconds, called 251736 times in my code.
My code:
BadMeas = unique([A; red2(B)']); where A and B are two column arrays, red2 is another row array.
3. ismember, took 7.117 seconds, called 269278 times in my code.
My code:
if ismember(selectedBus, InjBus_moreZeroLines)
for i = 1 : m
do the calculation
end
end
4. setdiff, called 13103 times in my code.
My code:
red = setdiff([1:length(A)],B);
A is an array and B is an array formed by integers.
I am wondering if there are faster functions to replace the above ones. Thanks.
Benson

댓글 수: 5

James Tursa
James Tursa 2021년 6월 18일
To get a meaningful answer, it would be better to post details about the overall problem you are solving and then also post the code you have written. Maybe there is a better way to solve the problem that doesn't call all of the functions you are currently calling.
Stephen23
Stephen23 2021년 6월 18일
편집: Stephen23 2021년 6월 18일
"I am wondering if there are faster functions to replace the above ones."
If there were otherwise identical faster functions that were drop-in replacements some slower functions... then why would anyone use the slower functions?
We need to consider the entire algorithm, not just some specific function calls. Upload your code by clicking the paperclip button.
Benson Gou
Benson Gou 2021년 6월 18일
Hi, Stephen,
Thanks for your prompt reply. My code has several thusand lines. I think it is hard to upload it.
Best regards,
Benson
dpb
dpb 2021년 6월 18일
편집: dpb 2021년 6월 18일
Cant' disagree w/ Stephen -- you would need, I think, to build a test case that has just the guts of the algorithm for anybody to be able to tell much...or describe the problem to be solved and perhaps somebody will have an alternate algorithm. Generally, real gains in speed are obtained by better/smarter algorithms than just trying to optimize existing code.
One thing I do note, however is
BadMeas = unique([A; red2(B)']);
has a set of square brackets and catenation inside what is described as a tight loop -- that may be a major part of the time there rather than unique() itself. Move that out if at all possible or do a direct assignment to a preallocated array instead of catenation.
Benson Gou
Benson Gou 2021년 6월 18일
Hi, dpb,
Thanks a lot for your reply. I will do as you suggested. You have a good weekend!
Benson

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2021년 6월 19일

0 개 추천

if ismember(selectedBus, InjBus_moreZeroLines)
ismember replies a vector, if the inputs are vectors. The if command requires a scalar condition. So I guess, this can be abbreviated. Maybe this is faster:
if any(selectedBus == InjBus_moreZeroLines)
In the line:
red = setdiff([1:length(A)],B)
the square brackets are a waste of time. [ ] is the concatenation operator, but here you concatenate the vector 1:length(A) with nothing.
Maybe this is more efficient:
red = true(size(A));
red(B) = false;
Then use red for logical indexing, instead of numerical indices.
I assume there are faster repalcements for the called functions, but without knowing what the inputs are and the context, it is not possible to suggest modifications.

댓글 수: 1

Benson Gou
Benson Gou 2021년 6월 21일
Hi, Jan,
Thanks a lot for your great help. I will test your suggestions. Once they work, I will accept your answers.
Best regards,
Benson

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2021년 6월 18일

댓글:

2021년 6월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by