matlab测试代码,测量快速执行的测试代码

在您的当前工作文件夹中创建一个基于类的测试,PreallocationTest.m,用于对不同的预分配方法进行比较。由于测试方法中包括验证,因此可以使用 startMeasuring 和 stopMeasuring 方法来定义要测量的代码的边界。

classdef PreallocationTest < matlab.perftest.TestCase

methods(Test)

function testOnes(testCase)

testCase.startMeasuring

x = ones(1,1e5);

testCase.stopMeasuring

testCase.verifyEqual(size(x),[1 1e5])

end

function testIndexingWithVariable(testCase)

import matlab.unittest.constraints.IsSameSetAs

testCase.startMeasuring

id = 1:1e5;

x(id) = 1;

testCase.stopMeasuring

testCase.verifyThat(x,IsSameSetAs(1))

end

function testIndexingOnLHS(testCase)

import matlab.unittest.constraints.EveryElementOf

import matlab.unittest.constraints.IsEqualTo

testCase.startMeasuring

x(1:1e5) = 1;

testCase.stopMeasuring

testCase.verifyThat(EveryElementOf(x),IsEqualTo(1))

end

function testForLoop(testCase)

testCase.startMeasuring

for i=1:1e5

x(i) = 1;

end

testCase.stopMeasuring

testCase.verifyNumElements(x,1e5)

end

end

end

运行 PreallocationTest 作为性能测试。滤除两个测试,因为测量结果太接近框架的精度。

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.

要指示框架自动遍历已测量的代码并计算测量结果平均值,请修改 PreallocationTest 以使用 keepMeasuring-while 循环,而不是使用 startMeasuring 和 stopMeasuring。

classdef PreallocationTest < matlab.perftest.TestCase

methods(Test)

function testOnes(testCase)

while(testCase.keepMeasuring)

x = ones(1,1e5);

end

testCase.verifyEqual(size(x),[1 1e5])

end

function testIndexingWithVariable(testCase)

import matlab.unittest.constraints.IsSameSetAs

while(testCase.keepMeasuring)

id = 1:1e5;

x(id) = 1;

end

testCase.verifyThat(x,IsSameSetAs(1))

end

function testIndexingOnLHS(testCase)

import matlab.unittest.constraints.EveryElementOf

import matlab.unittest.constraints.IsEqualTo

while(testCase.keepMeasuring)

x(1:1e5) = 1;

end

testCase.verifyThat(EveryElementOf(x),IsEqualTo(1))

end

function testForLoop(testCase)

while(testCase.keepMeasuring)

for i=1:1e5

x(i) = 1;

end

end

testCase.verifyNumElements(x,1e5)

end

end

end

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值