Main Content

matlab.perftest.TimeExperiment.limitingSamplingError

类: matlab.perftest.TimeExperiment
命名空间: matlab.perftest

为指定的误差界限和置信水平构造计时试验

说明

示例

experiment = matlab.perftest.TimeExperiment.limitingSamplingError 根据指定的统计目标(如误差界限和置信水平)为每个测试套件元素构造一个计时试验。此方法将返回 FrequentistTimeExperiment 的实例。此语法使用以下默认值来确定样本测量值的数量:

  • 预备测量值的数量:5

  • 最小样本数:4

  • 在未实现其他统计目标的事件中采集的最大样本数:256

  • 样本的目标相对误差界限:0.05 (5%)

  • 要在相对误差界限内的样本的置信水平:0.95 (95%)

示例

experiment = matlab.perftest.TimeExperiment.limitingSamplingError(Name,Value) 使用由一个或多个名称-值参量指定的其他选项构造一个计时试验。使用该语法可覆盖上面列出的默认值。

输入参数

全部展开

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: experiment = matlab.perftest.TimeExperiment.limitingSamplingError('RelativeMarginOfError',0.12,'MaxSamples',100) 创建一个采集样本测量值的计时试验,直到样本的相对误差界限达到 12% 或收集 100 个测量值为止。

预备测量值的数量,指定为非负整数。NumWarmups 定义测试框架运行测试代码进行预备的次数。

最小样本测量值数量,指定为正整数。该值定义框架在预备之后执行测试代码的最小次数。该框架至少将测试代码执行 MinSamples 次,而不管试验是否满足统计目标。

最大样本测量值数量,指定为正整数。该值定义框架在 NumWarmups 之后执行测试代码的最大次数。如果试验不满足统计目标,框架最多收集 MaxSamples 个测量值。

样本的目标相对误差界限,指定为正数。

框架使用以下方程计算样本 X 的相对误差界限:

relMoE=Tstd(X)mean(X)length(X)

其中 T 是使用指定的 ConfidenceLevellength(X)-1 自由度从 Student T 分布中获得的 T 分数。

样本位于相对误差界限内的置信水平,指定为介于 0 和 1 之间的数字。

示例

全部展开

在您的当前文件夹中创建一个基于类的测试,preallocationTest.m,用于对不同的预分配方法进行比较。

classdef preallocationTest < matlab.perftest.TestCase
    methods (Test)
        function testOnes(testCase)
            x = ones(1,1e7);
        end
        function testIndexingWithVariable(testCase)
            id = 1:1e7;
            x(id) = 1;
        end
        function testIndexingOnLHS(testCase)
            x(1:1e7) = 1;
        end
        function testForLoop(testCase)
            for i=1:1e7
                x(i) = 1;
            end
        end
    end
end

创建一个测试套件。

suite = testsuite("preallocationTest");

构造一个采集不定测量值样本数的计时试验,并运行这些测试。

import matlab.perftest.TimeExperiment
experiment = TimeExperiment.limitingSamplingError;
results = run(experiment,suite);
Running preallocationTest
.......... .......... .......... ......
Done preallocationTest
__________

查看第一个测试的测试活动。对于此测试,性能测试框架采集了 5 个预备测量值(默认值)和 4 个样本测量值。在 4 个样本测量值之后,框架满足了默认的统计目标。

results(1).TestActivity
ans =

  9×12 table

               Name               Passed    Failed    Incomplete    MeasuredTime    Objective         Timestamp             Host        Platform                 Version                            TestResult                         RunIdentifier            
    __________________________    ______    ______    __________    ____________    _________    ____________________    ___________    ________    __________________________________    ______________________________    ____________________________________

    preallocationTest/testOnes    true      false       false         0.016621       warmup      12-Oct-2022 16:19:12    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    a04b1262-f4c3-4af5-b68e-ae54a6ae24b1
    preallocationTest/testOnes    true      false       false         0.016783       warmup      12-Oct-2022 16:19:12    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    a04b1262-f4c3-4af5-b68e-ae54a6ae24b1
    preallocationTest/testOnes    true      false       false         0.016685       warmup      12-Oct-2022 16:19:12    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    a04b1262-f4c3-4af5-b68e-ae54a6ae24b1
    preallocationTest/testOnes    true      false       false         0.017241       warmup      12-Oct-2022 16:19:12    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    a04b1262-f4c3-4af5-b68e-ae54a6ae24b1
    preallocationTest/testOnes    true      false       false         0.017496       warmup      12-Oct-2022 16:19:12    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    a04b1262-f4c3-4af5-b68e-ae54a6ae24b1
    preallocationTest/testOnes    true      false       false         0.016733       sample      12-Oct-2022 16:19:12    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    a04b1262-f4c3-4af5-b68e-ae54a6ae24b1
    preallocationTest/testOnes    true      false       false         0.016654       sample      12-Oct-2022 16:19:12    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    a04b1262-f4c3-4af5-b68e-ae54a6ae24b1
    preallocationTest/testOnes    true      false       false         0.016602       sample      12-Oct-2022 16:19:12    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    a04b1262-f4c3-4af5-b68e-ae54a6ae24b1
    preallocationTest/testOnes    true      false       false         0.017102       sample      12-Oct-2022 16:19:12    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    a04b1262-f4c3-4af5-b68e-ae54a6ae24b1

构造一个计时试验,该试验采集 2 个预备测量值并运行不定次数的测试,直到样本均值达到 90% 置信水平的 10% 的相对误差界限目标。

experiment = TimeExperiment.limitingSamplingError("NumWarmups",2, ...
    "RelativeMarginOfError",0.10,"ConfidenceLevel",0.90);
results = run(experiment,suite);
Running preallocationTest
.......... .......... ....
Done preallocationTest
__________

查看第一个测试的测试活动。对于此测试,框架采集了 2 个预备测量值和 4 个样本测量值。在 4 个样本测量值之后,框架满足了指定的统计目标。

results(1).TestActivity
ans =

  6×12 table

               Name               Passed    Failed    Incomplete    MeasuredTime    Objective         Timestamp             Host        Platform                 Version                            TestResult                         RunIdentifier            
    __________________________    ______    ______    __________    ____________    _________    ____________________    ___________    ________    __________________________________    ______________________________    ____________________________________

    preallocationTest/testOnes    true      false       false         0.016576       warmup      12-Oct-2022 16:23:04    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    932317a8-bb61-4cba-945e-082f38eb0ced
    preallocationTest/testOnes    true      false       false         0.016709       warmup      12-Oct-2022 16:23:04    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    932317a8-bb61-4cba-945e-082f38eb0ced
    preallocationTest/testOnes    true      false       false         0.016672       sample      12-Oct-2022 16:23:04    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    932317a8-bb61-4cba-945e-082f38eb0ced
    preallocationTest/testOnes    true      false       false         0.017231       sample      12-Oct-2022 16:23:04    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    932317a8-bb61-4cba-945e-082f38eb0ced
    preallocationTest/testOnes    true      false       false         0.017646       sample      12-Oct-2022 16:23:04    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    932317a8-bb61-4cba-945e-082f38eb0ced
    preallocationTest/testOnes    true      false       false         0.016836       sample      12-Oct-2022 16:23:04    MY-HOSTNAME     win64      9.14.0.2078117 (R2023a) Prerelease    1×1 matlab.unittest.TestResult    932317a8-bb61-4cba-945e-082f38eb0ced

版本历史记录

在 R2016a 中推出

全部展开