MATLAB 性能测试与模拟对象创建指南
1. 使用脚本或函数进行性能测试
在 MATLAB 中,我们可以使用脚本或函数来进行性能测试,下面以向量预分配为例进行说明。
1.1 编写性能测试代码
在当前工作文件夹中创建一个名为 preallocationTest.m 的文件,其中包含脚本式测试或函数式测试代码。这里给出函数式测试的代码示例:
function tests = preallocationTest
tests = functiontests(localfunctions);
end
function testOnes(testCase)
vectorSize = getSize();
x = ones(1,vectorSize());
end
function testIndexingWithVariable(testCase)
vectorSize = getSize();
id = 1:vectorSize;
x(id) = 1;
end
function testIndexingOnLHS(testCase)
vectorSize = getSize();
x(1:vectorSize) = 1;
end
function testForLoop(testCase)
vectorSize = getSize();
for i=1:vectorSize
x(i) = 1;
end
end
function vectorSize = getS
超级会员免费看
订阅专栏 解锁全文
1万+

被折叠的 条评论
为什么被折叠?



