MATLAB在函数内处理大数据时有,global和参数传入两种方法,现对比其效率;
AirP.mat 是300000*218的double数组
1.global
function out = testFun1(p)
global AirP;
out = sum(AirP);
end
2. 参数传入
function out1 = testFun2(p,p2)
out1 = sum(p2);
end
3.主函数
load AirP.mat
tic
for i = 1:1000
p1 = testFun1(1);
end
toc
tic
for i = 1:1000
p1 = testFun2(1,AirP);
end
toc
结果
历时 0.007659 秒。
历时 16.464165 秒。
global 快