How can I decrease the runtime for a function (using cellfun or parfor)?
Show older comments
I am trying to speed some code up. Right now each iteration takes about 6 seconds to run and over the course of many iterations the runtime drastically increases. I tried two different running strategies: using cellfun and parallelization. Will paralization ever be slower than using cellfun? I get hugely varying times for the parfor on different computers due to different number of cores. Will the cellfun be more consistent (but slower) across computers? Below is my code. Thank you in advance.
Here is the function call:
%First way
testOut = cellfun(@testDFF_trial, num2cell(fReflect, 2), num2cell(fReflect2, 2), ...
num2cell(fSom, 2), num2cell(ones(size(fReflect, 1), 1) .* numFrames), 'UniformOutput', false);
%Second way
parfor numCell = 1:size(fReflect, 1)
dffAll{numCell} = testDFF(fReflect, fReflect2, fSom, numFrames, numCell); %slightly altered function for parfor compatibility
end
Here is the function that I am trying to speed up:
function dffTest = testDFF(fReflect, fReflect2, fSom, numFrames)
stepSize = 1;
windowSize = 900 / 2; %12
startLoc = 451; %20
endLoc = numFrames + 450; %80
%Gets the starting numbers
usedRange = startLoc:stepSize:endLoc;
%Gets the index that matter for each window
aOut = arrayfun(@(a, window) a-window:a+window, usedRange, ones(size(fReflect(usedRange))) .* windowSize, 'UniformOutput', false);
aOut2 = arrayfun(@(a, window) a-window:a+window, usedRange, ones(size(fReflect2(usedRange))) .* windowSize, 'UniformOutput', false);
%Makes ones array for later input
onesAll = (ones(1, length(fReflect)) .* fReflect);
onesAll2 = (ones(1, length(fReflect2)) .* fReflect2);
%Gets the values within each index
myfun = @(input, index) input(index);
datOut = cellfun(@(index) myfun(onesAll, index), aOut, 'UniformOutput', false);
datOut2 = cellfun(@(index) myfun(onesAll2, index), aOut2, 'UniformOutput', false);
%Find the 8th percentile
prcDat = cellfun(@prctile, datOut, num2cell(ones(1, length(datOut)) .* 8));
prcDat2 = cellfun(@prctile, datOut2, num2cell(ones(1, length(datOut2)) .* 8));
%Find the dff
dffFunct = @(rawF, prcF, baseline) (rawF - prcF) ./ baseline;
dffTest = arrayfun(dffFunct, fSom, prcDat, prcDat2);
tInter = toc;
display(['Cell processed - time: ', num2str(tInter / 60, '%0.2f')]);
end
4 Comments
"I am trying to speed some code up."
Then get rid of all of those ARRAYFUN and CELLFUN calls. One FOR-loop would be faster. Then you could also avoid things like NUM2CELL: rather than forcing MATLAB to duplicate the data in lots of separate arrays, just use a simple FOR-loop and indexing into some numeric arrays.
"I tried two different running strategies: using cellfun..."
In general CELLFUN is slower than a well-written FOR-loop. Ans you have lots of CELLFUN :(
"... and parallelization"
is no replacement for writing more efficient code in the first place: e.g. replacing all of those ARRAYFUN, CELLFUN, NUM2CELL, etc with a FOR-loop.
AES
on 12 Mar 2024
Steven Lord
on 12 Mar 2024
FYI, another tool to add to your programming arsenal is the Profiler. This can help you understand what the most expensive location or segment of your code is. Using that you can also experiment with different approaches to determine if a change is better, worse, or the same in terms of performance. [That reminds me of my last eye exam ;)]
AES
on 12 Mar 2024
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!