这个示例展示了如何创建一个基于脚本或函数的性能测试,该测试使用四种不同的方法对向量进行预分配。
在文件中创建一个性能测试,preallocationTest.m
,在当前工作文件夹中。在本例中,您可以选择使用以下基于脚本的测试或基于函数的测试。本例中的输出是针对基于函数的测试的。如果您使用基于脚本的测试,那么您的测试名称将会不同。
基于脚本的性能测试 | 基于函数的性能测试 |
---|---|
vectorSize = 1 e7;% %的函数x = 1(1、vectorSize);%%变量索引id = 1: vectorSize;x (id) = 1;%% LHS索引x (1: vectorSize) = 1;% % For循环为i=1:vectorSize x(i) =1;结束 |
函数test = preallocationTest测试= functiontests(localfunctions);结束函数testOnes(testCase) vectorSize = getSize();x = 1 (vectorSize ());结束函数testIndexingWithVariable(testCase) vectorSize = getSize();id = 1: vectorSize;x (id) = 1;结束函数testIndexingOnLHS(testCase) vectorSize = getSize();x (1: vectorSize) = 1;结束函数testForLoop(testCase) vectorSize = getSize();为i=1:vectorSize x(i) =1;结束结束函数vectorSize = getSize() vectorSize = 1e7;结束 |
使用runperf
.
结果= runperf (“preallocationTest.m”)
运行preallocationTest .......... .......... .......... ..Done preallocationTest __________ results = 1×4 TimeResult array with properties: Name Valid Samples TestActivity Totals: 4 Valid, 0 Invalid. 10.2561秒的测试时间。
的结果
变量是一个1
——- - - - - -4
TimeResult
数组中。中代码部分中定义的一个测试对应于数组中的每个元素preallocationTest.m
.
显示第二次测试的测量结果。结果可能会有所不同。
结果(2)
ans = timerresult with properties: Name: 'preallocationTest/testIndexingWithVariable' Valid: 1 Samples: [4×7 table] TestActivity: [8×12 table]总计:1 Valid, 0 Invalid. 1.2274秒测试时间。
如尺寸所示TestActivity
属性,收集的性能测试框架8
测量。这个数量的度量包括四个度量来预热代码。的样品
属性不包括预热测量。
显示第二次测试的样本测量值。
(2)结果。样品
> >(2)的结果。样品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
显示第二次测试的平均测量时间。要排除在预热运行中收集的数据,请使用样品
字段。
sampleTimes = (2) .Samples.MeasuredTime结果;meanTest2 =意味着(sampleTimes)
meanTest2 = 0.1534
性能测试框架为第二次测试收集了四个样例度量。测试的平均值是0.1534
第二。
确定所有测试元素的平均时间。的preallocationTest
Test包含四种不同的方法来分配一个由1组成的向量。比较每个方法(测试元素)的时间。
因为性能测试框架返回一个样品
表,将所有这些表连接到一个表中。然后根据测试元素对行进行分组的名字
,计算平均值MeasuredTime
为每个组。
fullTable = vertcat (results.Samples);summaryStats = varfun (@mean fullTable,...“数据源”,“MeasuredTime”,“GroupingVariables”,“名字”)
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
函数通过构造和运行一个时间实验。构建一个时间实验,测量值达到一个样本平均值8%
相对误差范围在97%
信心水平。
构造一个明确的测试套件。
套件= testsuite (“preallocationTest”);
使用不同数量的样本测量值构建时间实验,并运行测试。
进口matlab.perftest.TimeExperiment实验= TimeExperiment.limitingSamplingError (“NumWarmups”2,...“RelativeMarginOfError”, 0.08,“ConfidenceLevel”, 0.97);resultsTE =运行(实验套件);
运行preallocationTest .......... .......... ....完成preallocationTest __________
计算所有测试元素的统计数据。
fullTableTE = vertcat (resultsTE.Samples);summaryStatsTE = varfun (@mean fullTableTE,...“数据源”,“MeasuredTime”,“GroupingVariables”,“名字”)
summaryStatsTE = 4×3表名GroupCount mean_MeasuredTime __________________________________________ __________ _________________ preallocationTest /头像硬币4 0.040484 preallocationTest / testIndexingWithVariable 4 0.15187 preallocationTest / testIndexingOnLHS 4 0.046224 preallocationTest / testForLoop 4 1.0262
runperf
|testsuite
|matlab.perftest.TimeExperiment
|matlab.perftest.TimeResult
|matlab.unittest.measurement.DefaultMeasurementResult