Why is large matrix subtraction making my code so slow?
显示 更早的评论
Hello,
I have created a function which handles matrices roughly [10000 x 20000] in size. When I ran my function using the time & run profiler in MatLab, it is telling me that two lines of my code are taking 29s and 22s respectively:

These lines of code are basic matrix operations that shouldn't be computationally expensive to perform. They are equivalent to:
N = rand(10000,20000);
X = [zeros(10000,1) cumsum(N,2)];
M = X(:,5:end) - X(:,1:end-5);
When I run these lines of code in the command window, they only take a second or two at most.
Can anyone please tell me why MatLab is taking so long to calculate these lines inside a function? Thanks a mil!
3 个评论
the cyclist
2018-9-18
编辑:the cyclist
2018-9-18
Are you able to distill your code down to something that exhibits the problem, and post it here? For example, you could save the workspace as it exists just before this code is run (really, just the relevant variables) into a MAT file, and post the few lines of code that are taking longer than you expect.
You might find that through this distillation process, you actually discover what the issue is.
John D'Errico
2018-9-18
You may be executing those lines MANY times. So while ONE call there will be relatively fast, the total time for those lines is 20-30 seconds. The profile tool tells you the total time spent on those lines, not for ONE call.
If you are truly executing those lines ONLY once, then the odds are good that they are sufficiently large enough arrays that they are forcing MATLAB to farm memory out to disk, thus going into virtual memory.
Matt Flood
2018-9-27
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!