主要内容

测量快速执行测试代码

Performance tests that execute too quickly for MATLAB®to measure accurately are filtered with an assumption failure. With thekeepMeasuring方法,测试框架可以通过自动确定通过代码迭代的时间和测量平均性能来衡量代码的尺寸更快。

在您当前的工作文件夹中,创建一个基于类的测试,PreallocationTest.m, that compares different methods of preallocation. Since the test methods include qualifications, use the起步停止methods to define boundaries for the code you want to measure.

classdefPreallocationTest < matlab.perftest.TestCasemethods(测试)功能testones(testCase)testCase.StartMeasing X =一个(1,1E5);testCase.Stopmeasuring testcase.verifyequal(尺寸(x),[1 1E5])end功能testIndexingWithVariable(testCase) importmatlab.unittest.constraints.IsSameSetAstestCase.startMeasuring id = 1:1e5; x(id) = 1; testCase.stopMeasuring testCase.verifyThat(x,IsSameSetAs(1))end功能testIndexingOnLHS(testCase) importmatlab.unittest.constraints.EveryElementOfimportmatlab.unittest.constraints.IsEqualTotestCase.startMeasuring x(1:1e5) = 1; testCase.stopMeasuring testCase.verifyThat(EveryElementOf(x),IsEqualTo(1))end功能testForLoop(testCase) testCase.startMeasuringfori=1:1e5 x(i) = 1;endtestCase.stopMeasuring testCase.verifyNumElements(x,1e5)endendend

RunPreallocationTestas a performance test. Two tests are filtered because the measurements are too close to the precision of the framework.

results = runperf('PreallocationTest');
Running PreallocationTest ........ ================================================================================ PreallocationTest/testOnes was filtered. Test Diagnostic: The MeasuredTime should not be too close to the precision of the framework. Details ================================================================================ .. .......... .... ================================================================================ PreallocationTest/testIndexingOnLHS was filtered. Test Diagnostic: The MeasuredTime should not be too close to the precision of the framework. Details ================================================================================ ...... .. Done PreallocationTest __________ Failure Summary: Name Failed Incomplete Reason(s) ================================================================================== PreallocationTest/testOnes X Filtered by assumption. ---------------------------------------------------------------------------------- PreallocationTest/testIndexingOnLHS X Filtered by assumption.

要指示框架自动循环浏览测得的代码和平均测量结果,请修改PreallocationTestto use a保持态度loop instead of起步停止.

classdefPreallocationTest < matlab.perftest.TestCasemethods(测试)功能testOnes(testCase)尽管(testcase.保存)x = ones(1,1e5);endtestCase.verifyEqual(size(x),[1 1e5])end功能testIndexingWithVariable(testCase) importmatlab.unittest.constraints.IsSameSetAs尽管(testcase.保存)id = 1:1e5; x(id) = 1;endtestcase.verifythat(X,Issamesetas(1))end功能testIndexingOnLHS(testCase) importmatlab.unittest.constraints.EveryElementOfimportmatlab.unittest.constraints.IsEqualTo尽管(testcase.kepmemeasing)x(1:1e5)= 1;endtestcase.verifythat((x),isequalto(1))end功能testForLoop(testCase)尽管(testcase.保存)fori=1:1e5 x(i) = 1;endendtestCase.verifyNumElements(x,1e5)endendend

重新运行测试。所有测试完成。

results = runperf('PreallocationTest');
Running PreallocationTest .......... .......... .......... .. Done PreallocationTest __________

查看结果。

sampleSummary(results)
ans = 4×7 table Name SampleSize Mean StandardDeviation Min Median Max __________________________________________ __________ __________ _________________ __________ __________ __________ PreallocationTest/testOnes 4 3.0804e-05 1.8337e-07 3.0577e-05 3.0843e-05 3.0953e-05 PreallocationTest/testIndexingWithVariable 4 0.00044536 1.7788e-05 0.00042912 0.00044396 0.00046441 PreallocationTest/testIndexingOnLHS 4 5.6352e-05 1.8863e-06 5.5108e-05 5.5598e-05 5.9102e-05 PreallocationTest/testForLoop 4 0.0097656 0.00018202 0.0096181 0.0097065 0.010031

See Also

|

相关话题