使用脚本或函数测试性能
以下示例演示如何创建并运行基于脚本或函数的性能测试,使用四种不同方法计算向量预分配耗用的时间。
编写性能测试
在当前文件夹中名为 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; end | 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 = 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.
8.7168 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: [9×12 table]
Totals:
1 Valid, 0 Invalid.
0.87973 seconds testing time.如 TestActivity 属性的大小所示,性能测试框架采集了 9 个测量值。此测量值个数包括五个测量值来预备代码。Samples 属性会排除预备测量值。
显示第二个测试的样本测量值。
results(2).Samples
ans =
4×7 table
Name MeasuredTime Timestamp Host Platform Version RunIdentifier
__________________________________________ ____________ ____________________ ___________ ________ __________________________________ ____________________________________
preallocationTest/testIndexingWithVariable 0.096513 14-Oct-2022 14:04:00 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 7ff84b09-8a58-4961-bfcb-75c86f4a3ae1
preallocationTest/testIndexingWithVariable 0.097008 14-Oct-2022 14:04:00 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 7ff84b09-8a58-4961-bfcb-75c86f4a3ae1
preallocationTest/testIndexingWithVariable 0.096777 14-Oct-2022 14:04:00 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 7ff84b09-8a58-4961-bfcb-75c86f4a3ae1
preallocationTest/testIndexingWithVariable 0.097157 14-Oct-2022 14:04:00 MY-HOSTNAME win64 9.14.0.2081372 (R2023a) Prerelease 7ff84b09-8a58-4961-bfcb-75c86f4a3ae1
计算单个测试元素的统计信息
显示第二个测试的测量时间均值。要排除在预备运行中采集的数据,请使用 Samples 属性中的值。
sampleTimes = results(2).Samples.MeasuredTime; meanTest2 = mean(sampleTimes)
meanTest2 =
0.0969计算所有测试元素的统计信息
要比较不同预分配方法,请基于 results 创建一个摘要统计量表。在此示例中,ones 函数是将向量值全部初始化为 1 的最快方法。性能测试框架为此测试进行了四次测量运行。
T = sampleSummary(results)
T =
4×7 table
Name SampleSize Mean StandardDeviation Min Median Max
__________________________________________ __________ ________ _________________ ________ ________ ________
preallocationTest/testOnes 4 0.016716 0.00018455 0.016515 0.016699 0.016952
preallocationTest/testIndexingWithVariable 4 0.096864 0.0002817 0.096513 0.096892 0.097157
preallocationTest/testIndexingOnLHS 15 0.024099 0.0025168 0.022721 0.0232 0.031685
preallocationTest/testForLoop 4 0.79044 0.016054 0.78203 0.78261 0.81452更改统计目标并重新运行测试
通过构造和运行计时试验来更改 runperf 函数定义的统计目标。构造一个计时试验,该试验采集 2 个预备测量值并运行不定次数的测试,直到样本均值达到 98% 置信水平的 4% 的相对误差界限目标。
创建一个测试套件。
suite = testsuite("preallocationTest");根据指定的需求构造一个计时试验,并运行测试套件。
import matlab.perftest.TimeExperiment experiment = TimeExperiment.limitingSamplingError("NumWarmups",2, ... "RelativeMarginOfError",0.04,"ConfidenceLevel",0.98); resultsTE = run(experiment,suite);
Running preallocationTest .......... .......... .......... .......... Done preallocationTest __________
计算所有测试元素的摘要统计量。
T1 = sampleSummary(resultsTE)
T1 =
4×7 table
Name SampleSize Mean StandardDeviation Min Median Max
__________________________________________ __________ ________ _________________ ________ ________ ________
preallocationTest/testOnes 16 0.017424 0.001223 0.016911 0.017056 0.021938
preallocationTest/testIndexingWithVariable 8 0.099153 0.0039523 0.096619 0.097218 0.10736
preallocationTest/testIndexingOnLHS 4 0.022985 0.00018664 0.022843 0.022921 0.023257
preallocationTest/testForLoop 4 0.80613 0.005993 0.79979 0.80591 0.8129另请参阅
runperf | testsuite | matlab.perftest.TimeExperiment | matlab.perftest.TimeResult