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
— 预备测量值的数量
5 (默认) | 非负整数
预备测量值的数量,指定为非负整数。NumWarmups
定义测试框架运行测试代码进行预备的次数。
MinSamples
— 最小样本数
4 (默认) | 正整数
最小样本测量值数量,指定为正整数。该值定义框架在预备之后执行测试代码的最小次数。该框架至少将测试代码执行 MinSamples
次,而不管试验是否满足统计目标。
MaxSamples
— 最大样本数
256 (默认) | 正整数
最大样本测量值数量,指定为正整数。该值定义框架在 NumWarmups
之后执行测试代码的最大次数。如果试验不满足统计目标,框架最多收集 MaxSamples
个测量值。
RelativeMarginOfError
— 样本的目标相对误差界限
0.05 (默认) | 正数
样本的目标相对误差界限,指定为正数。
框架使用以下方程计算样本 X
的相对误差界限:
其中 T
是使用指定的 ConfidenceLevel
和 length(X)-1
自由度从 Student T 分布中获得的 T 分数。
ConfidenceLevel
— 要在相对误差界限内的样本的置信水平
0.95 (默认) | 介于 0 和 1 之间的数字
样本位于相对误差界限内的置信水平,指定为介于 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 中推出R2023a: 预备测量值的默认次数已增加
框架在不定次计时试验中对测试代码进行预备的默认次数从 4 次增加到 5 次。这种变化通常会导致满足目标相对误差界限所需的样本更少。
如果希望使用以前的默认值,请在代码中显式指定该值。下表显示如何更新使用四个预备测量值运行测试的代码示例。
更新前 | 更新后 |
---|---|
import matlab.perftest.TimeExperiment
experiment = TimeExperiment.limitingSamplingError;
results = run(experiment,mySuite); | import matlab.perftest.TimeExperiment experiment = TimeExperiment.limitingSamplingError("NumWarmups",4); results = run(experiment,mySuite); |
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)