How Can I Speed Up My Code
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
More Answers (1)
Jan
on 10 Oct 2012
Edited: Jan
on 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.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!