编写性能测试
在您的当前工作文件夹下的文件中创建一个性能测试 preallocationTest.m。在此示例中,您可以选择使用以下基于脚本的测试或基于函数的测试。此示例中的输出适用于基于函数的测试。如果您使用基于脚本的测试,则测试名称会有所不同。
基于脚本的性能测试基于函数的性能测试vectorSize = 1e7;
%% Ones Function
x = ones(1,vectorSize);
%% Indexing With Variable
id = 1:vectorSize;
x(id) = 1;
%% Indexing On LHS
x(1:vectorSize) = 1;
%% For Loop
for i=1:vectorSize
x(i) = 1;
endfunction 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 = getSize()
vectorSize = 1e7;
end
运行性能测试
使用 runperf 运行性能测试。
results = runperf('preallocationTest.m')
Running preallocationTest
.......... .......... .......... ..
Done preallocationTest
__________
results =
1×4 TimeResult array with properties:
Name
Valid
Samples
TestActivity
Totals:
4 Valid, 0 Invalid.
10.2561 seconds testing time.
results 变量是 1×4 TimeResult 数组。数组中的每个元素对应于 preallocationTest.m 的代码节中定义的一个测试。
显示测试结果
显示第二个测试的测量结果。您的结果可能有所不同。
results(2)
ans =
TimeResult with properties:
Name: 'preallocationTest/testIndexingWithVariable'
Valid: 1
Samples: [4×7 table]
TestActivity: [8×12 table]
Totals:
1 Valid, 0 Invalid.
1.2274 seconds testing time.
如 TestActivity 属性的大小所示,性能测试框架采集了 8 个测量值。此测量值个数包括四个测量值来预备代码。Samples 属性会排除预备测量值。
显示第二个测试的样本测量值。
results(2).Samples
>> results(2).Samples
ans =
4×7 table
Name MeasuredTime Timestamp Host Platform Version RunIdentifier
__________________________________________ ____________ ____________________ ___________ ________ __________________________________________ ____________________________________
preallocationTest/testIndexingWithVariable 0.15271 24-Jun-2019 16:13:33 MY-HOSTNAME win64 9.7.0.1141441 (R2019b) Prerelease Update 2 9a6ee247-b49d-4e29-8b63-ba13c2eead27
preallocationTest/testIndexingWithVariable 0.15285 24-Jun-2019 16:13:33 MY-HOSTNAME win64 9.7.0.1141441 (R2019b) Prerelease Update 2 9a6ee247-b49d-4e29-8b63-ba13c2eead27
preallocationTest/testIndexingWithVariable 0.15266 24-Jun-2019 16:13:33 MY-HOSTNAME win64 9.7.0.1141441 (R2019b) Prerelease Update 2 9a6ee247-b49d-4e29-8b63-ba13c2eead27
preallocationTest/testIndexingWithVariable 0.15539 24-Jun-2019 16:13:34 MY-HOSTNAME win64 9.7.0.1141441 (R2019b) Prerelease Update 2 9a6ee247-b49d-4e29-8b63-ba13c2eead27
计算单个测试元素的统计信息
显示第二个测试的测量时间均值。要排除在预备运行中采集的数据,请使用 Samples 字段中的值。
sampleTimes = results(2).Samples.MeasuredTime;
meanTest2 = mean(sampleTimes)
meanTest2 =
0.1534
性能测试框架为第二个测试采集了 4 个样本测量值。该测试的平均运行时间为 0.1534 秒。
计算所有测试元素的统计信息
确定所有测试元素的平均时间。preallocationTest 测试包括四种不同方法,用于分配由 1 组成的向量。比较每个方法(测试元素)的时间。
因为性能测试框架为每个测试元素返回一个 Samples 表,并将所有这些表串联成一个表。然后按测试元素 Name 对行分组,并计算每个组的平均 MeasuredTime。
fullTable = vertcat(results.Samples);
summaryStats = varfun(@mean,fullTable,...
'InputVariables','MeasuredTime','GroupingVariables','Name')
summaryStats =
4×3 table
Name GroupCount mean_MeasuredTime
__________________________________________ __________ _________________
preallocationTest/testOnes 4 0.041072
preallocationTest/testIndexingWithVariable 4 0.1534
preallocationTest/testIndexingOnLHS 4 0.04677
preallocationTest/testForLoop 4 1.0343
更改统计目标并重新运行测试
通过构造和运行计时试验来更改 runperf 函数定义的统计目标。构造所采集测量值的样本均值达到 97% 置信水平的 8% 相对误差界限目标的计时试验。
构造一个显式测试套件。
suite = testsuite('preallocationTest');
构造一个采集不定测量值样本数的计时试验,并运行这些测试。
import matlab.perftest.TimeExperiment
experiment = TimeExperiment.limitingSamplingError('NumWarmups',2,...
'RelativeMarginOfError',0.08, 'ConfidenceLevel', 0.97);
resultsTE = run(experiment,suite);
Running preallocationTest
.......... .......... ....
Done preallocationTest
__________
计算所有测试元素的统计信息。
fullTableTE = vertcat(resultsTE.Samples);
summaryStatsTE = varfun(@mean,fullTableTE,...
'InputVariables','MeasuredTime','GroupingVariables','Name')
summaryStatsTE =
4×3 table
Name GroupCount mean_MeasuredTime
__________________________________________ __________ _________________
preallocationTest/testOnes 4 0.040484
preallocationTest/testIndexingWithVariable 4 0.15187
preallocationTest/testIndexingOnLHS 4 0.046224
preallocationTest/testForLoop 4 1.0262