How Can I Speed Up My Code

2 vues (au cours des 30 derniers jours)
Brian
Brian le 10 Oct 2012
I've got a job that is running a bunch of iterations on a large dataset and taking more time than I'd like. While I'd like to write the job to use ParFor that's too much work at the moment because of how things are written currently. I'm hoping to make a few quick tweaks to speed up the code. When I run the profiler I see 3 self made Calls that are taking up 80% of the run time. Two of the calls are slow because of appending to unallocated variables, which I will fix but the slowest of the 3 is below. I am just wondering if this call can be made more effient.
40% of project total Run Time CurSL = SPReturns_Trl(:,i) - SP_TR_Trl(i) < SL_Amount(sla);
Where SPReturns_Trl = 9813x5742 Double and SP_TR_Trl = 1x5742 Double.
SL_Amount is just a 1x6 Double.
Thanks a lot, Brian

Réponse acceptée

Matt J
Matt J le 10 Oct 2012
Instead of looping over i, perhaps vectorize using BSXFUN
CurSLTotal=bsxfun(@lt,SPReturns, SP_TR_Trl + SL_Amount(sla));
  2 commentaires
Brian
Brian le 11 Oct 2012
I ended up with the following 3 lines of code that are much faster in aggregate that the old for loop.
Doing the subtraction and less than in aggregate rather than one column at a time.
Differences = bsxfun(@minus,SPReturns_Trl,SP_TR_Trl);
Differences = bsxfun(@lt,Differences,SL_Amount(sla));
Inside the for loop (for each day) I choose the proper logical index value from the Differences index created above.
CurSL = Differences(:,i);
Matt J
Matt J le 11 Oct 2012
Okay, though, it's not entirely clear to me why this required 2 calls to bsxfun, rather than the single call I proposed.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 10 Oct 2012
Modifié(e) : Jan le 10 Oct 2012
Do not forget, that the profiler disables the JIT acceleration. Therefore the measurements can have a strong bias. Some TIC/TOCs are smarter.
CurSL = SPReturns_Trl(:,i) - SP_TR_Trl(i) < SL_Amount(sla)
Matlab computes statements from left to right:
tmp1 = SPReturns_Trl(:,i) - SP_TR_Trl(i); % vector operation
CurSL = tmp1 < SL_Amount(sla); % vector operation
When I assume, that "i" and "sla" are scalar counters (please explain the necessary details...), one vector operation can be omitted:
CurSL = SPReturns_Trl(:,i) < (SL_Amount(sla) + SP_TR_Trl(i));
Now we get:
tmp1 = (SL_Amount(sla) + SP_TR_Trl(i)); % Scalar
CurSL = SPReturns_Trl(:,i) < tmp1; % Vector
However, optimizing a single line taken out of its context is not reliable. If the missing pre-allocation causes disk swapping, it can happen, that a lot of time is spent during the shown command is processed, but not because of the command.
So fix the severe pre-allocation problems at first to avoid the famous anti-pattern of pre-mature optimization.
  1 commentaire
Brian
Brian le 10 Oct 2012
Modifié(e) : Brian le 11 Oct 2012
tic and toc would be nice, but the reason this specific loop is slow is that I'm running it 5742*630 times or 3.6 million times. I guess in this case tic and toc may not help me much.
Both of your suggestions may help me. I will have more time to address first thing tomorrow when I'm back in the office. Jan, the second part of your answer is around 2-4 feet over my head. I am an Investments person, not a programmer by trade. That's not to say that I don't wish I understood what that meant, but currently I do not.
Thanks for the help guys.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by