【发布时间】:2016-01-12 19:34:30
【问题描述】:
(以下发生在配备 16GB RAM 的 Macbook Pro 2.3GHz i7 Haswell 上)
问题
在 Matlab 中,我有一个 3D 数据处理算法。在这种情况下,一个三向嵌套循环,其中维度的范围为每个向量
当我将同样的代码(复制和粘贴)移动到由 Matlab 生成的 GUIDE GUI 回调函数(按钮)中时,执行速度会减慢到绝对爬行。这不是正常比率的 1/10 甚至 1/100。可能它以正常利率的 1/1000 收盘......或者类似的东西。
想法
所以我最初的想法是,这种激进的性能冲击一定来自 GUI。所以我尝试实现parfor Matlab 关键字,以便并行运行计算密集型的 for 循环。这是不可能的,因为在parfor-loop 中创建的任何变量都不能在它之外使用。或者至少我是这样解释我在尝试使用该方法时遇到的 Matlab 错误的。
我还尝试在新线程中生成计算,但似乎并没有以任何方式提高性能。
问题
我的问题:从 Matlab GUI 运行时,是否有某种方式可以执行计算密集型任务?
加法
这种情况的伪代码。在循环中使用了以下 Matlab 函数:max()、abs()、sqrt()、zeros()、mod()、fprintf() 请注意 fprintf() 的使用并不过分,我知道对此类函数的过多调用可能会导致性能下降。
for-loop
fprintf(1,'%d ',i);
if (mod(i,20)==0)
fprintf(1,'\n'); % output iteration status
end
for-loop
for-loop
some variable assignments
declare variables and init them to 1 or 0
for-loop Xvar
if ((condition)) <= 0.0
variable assignment
end
if ((condition)) <= 0.0
variable assignment
end
end
for-loop Yvar
if ((condition)) <= 0.0
variable assignment
end
if ((condition)) <= 0.0
variable assignment
end
end
for-loop Zvar
if ((condition)) <= 0.0
variable assignment
end
if ((condition)) <= 0.0
variable assignment
end
end
somevariable=zeros(args);
for-loop
multiplication assignment to variable
multiplication assignment to variable
variable assignment
for-loop
for-loop
for-loop
sqrt calculation with some divisions and squares
if-statement
subtraction addition assignment
if-statement
var=some other var;
end
end
end
end
end
somevarindexed(m)=stuff calculated above;
end
variable assignment;
3Dvector(x,y,z) assignment;
3Dvector(x,y,z) assignment;
end
end
end
【问题讨论】:
-
我无法复制该行为。你的计算是递归的吗?初始代码是否运行到脚本或函数中?如果是函数,那么包含计算代码的回调是嵌套函数吗?
-
我在帖子中添加了伪代码。调用顺序为:GUIDE GUI Handler -> Button Callback with algorithm inside。没有进行其他函数调用(除了我在上面添加的 Matlab 函数)。
-
您有没有used the profiler 确定这两种情况在执行上的差异?
-
还要检查在 GUI 中运行内存时与没有 GUI 时内存发生的情况。 MATLAB GUI 并不是完全优化的,所以如果 GUI 占用的内存刚好足以在循环运行时停止一切,我也不会感到惊讶。
标签: performance matlab user-interface matlab-guide