Speeding up the nested for loops
显示 更早的评论
Hi,
I have a nested for loop that inside the innermost loop, the result of each iteration should be saved in a 4D matrix.
Althought the calculation is more accurate than curve fitting but the speed is awful. How can I speed up this piece of code? Parloop fails due to the way I save the data.

9 个评论
KSSV
2022-11-4
Things to consider:
- Is really loop required?
- Can't it be vectorsed?
- Did you initialize the arrays filling in loop?
Zahra Yousefi Darani
2022-11-4
Raymond Norris
2022-11-4
I'm puzzled how DistChoiceAllParams isn't overwritten by the last iteration of N? That is, you run line 99 SampN times (i.e., 1000). No matter what the value of DistChoiceAllParams is after the 1st 999 iterations, the last time through the Murange loop is what will be assigned to DistChoiceAllParams, no?
Unless you're doing something on line 109 (e.g., plotting DistChoiceAllParams or writing DistChoiceAllParams to a file), I don't see the purpose of the outer for-loop.
Zahra Yousefi Darani
2022-11-4
Santosh Fatale
2022-11-8
Hi Zahra,
Could you share the sample code?
Marcel
2022-11-8
By having so many for loops, its not surprising me that its being slow, just from first sight. Without knowing what the code actually does we cant help you in investigating on what might take a lot of resources/time to execute.
Zahra Yousefi Darani
2022-11-8
编辑:Bruno Luong
2022-11-8
We cannot run this code due to the missing input files. Then it is very hard to improve code, which is written as a large monolithic block, because we have to guess, where the bottleneck is.
Split the code into functions. Use a function as main part also instead of using the brute clearing header close all, clear,clc. Avoid clear of variabels, because this is usually (but not in all cases) a waste of time in Matlab.
Avoid the izterative growing of arrays, because this is very expensive:
NeededStim = zeros(SampN, ???); % Pre-allocate accordingly!!!
NeededChoice = zeros(SampN, ???); % Pre-allocate accordingly!!!
for N = 1 : SampN
SampleID = randsample([1:numel(SessID)],numel(SessID),true);
SampleStim = zeros(numel(SampleID), 250);
SampleChoice = zeros(numel(SampleID), 250);
for ss = 1 : numel(SampleID)
SampleStim(:, ss) = data(SessID(SampleID(ss)):SessID(SampleID(ss))+249,8);
SampleChoice(:, ss) = data(SessID(SampleID(ss)):SessID(SampleID(ss))+249,6);
end
NeededStim(N,:) = SampleStim(:);
NeededChoice(N,:) = SampleChoice(:);
end
This is ugly:
feval('assignin','base',name,Params)
Because this is a script, you create a variable dynamically in the local workspace. As eval this impedes Matlab's JIT acceleration drastically: Afterwards Matlab has to check the look-up table of variables instead of using efficient pointers to the values. This can slow down loops by a factor of 100. "Optimizing" this code is not the point inthis case, but cleaing it from evil pitfalls.
Zahra Yousefi Darani
2022-11-8
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!