Main Content

matlab.perftest.TimeExperiment.withFixedSampleSize

类: matlab.perftest.TimeExperiment
包: matlab.perftest

构造采集固定数量的测量值的计时试验

说明

示例

experiment = matlab.perftest.TimeExperiment.withFixedSampleSize(numSamples) 构造采集固定数量的测量值的计时试验。此方法将返回 FixedTimeExperiment 的实例。

experiment = matlab.perftest.TimeExperiment.withFixedSampleSize(numSamples,'NumWarmups',numWarmups) 将计时试验配置为先预备执行代码 numWarmups 次。

输入参数

全部展开

要采集的样本测量值的数量,指定为正整数。如果指定了预备执行次数,测试框架将先执行代码 numWarmups 次,再采集 numSamples 个测量值。

预备测量值的数量,指定为非负整数。numWarmups 定义测试框架运行测试代码进行预备的次数。通过预备执行代码,可对典型的执行时间进行更切合实际的分析,因为这可以最大程度减少首次运行成本的影响。

示例: experiment = matlab.perftest.TimeExperiment.withFixedSampleSize(24,'NumWarmups',8) 构造一个 FixedTimeExperiment,它先预备执行代码 8 次,然后执行 24 次以采集样本测量值。

示例

全部展开

在您的当前工作文件夹中创建一个基于类的测试,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
numSamples = 6;
experiment = TimeExperiment.withFixedSampleSize(numSamples);
result = run(experiment,suite);
Running preallocationTest
..........
..........
....
Done preallocationTest
__________

查看第四个测试的测试活动。

result(4).TestActivity
ans = 

                Name                 Passed    Failed    Incomplete    MeasuredTime    Objective         Timestamp             Host        Platform           Version                      TestResult                          RunIdentifier            
    _____________________________    ______    ______    __________    ____________    _________    ____________________    ___________    ________    _____________________    ________________________________    ____________________________________

    preallocationTest/testForLoop    true      false     false         0.90553         sample       29-Dec-2015 12:14:55    MY-HOSTNAME    win64       9.0.0.320924 (R2016a)    [1x1 matlab.unittest.TestResult]    a07f34c0-5653-4e01-b814-118fe30d3adf
    preallocationTest/testForLoop    true      false     false         0.86564         sample       29-Dec-2015 12:14:56    MY-HOSTNAME    win64       9.0.0.320924 (R2016a)    [1x1 matlab.unittest.TestResult]    a07f34c0-5653-4e01-b814-118fe30d3adf
    preallocationTest/testForLoop    true      false     false         0.75888         sample       29-Dec-2015 12:14:57    MY-HOSTNAME    win64       9.0.0.320924 (R2016a)    [1x1 matlab.unittest.TestResult]    a07f34c0-5653-4e01-b814-118fe30d3adf
    preallocationTest/testForLoop    true      false     false         0.74051         sample       29-Dec-2015 12:14:58    MY-HOSTNAME    win64       9.0.0.320924 (R2016a)    [1x1 matlab.unittest.TestResult]    a07f34c0-5653-4e01-b814-118fe30d3adf
    preallocationTest/testForLoop    true      false     false          0.8735         sample       29-Dec-2015 12:14:58    MY-HOSTNAME    win64       9.0.0.320924 (R2016a)    [1x1 matlab.unittest.TestResult]    a07f34c0-5653-4e01-b814-118fe30d3adf
    preallocationTest/testForLoop    true      false     false         0.83188         sample       29-Dec-2015 12:14:59    MY-HOSTNAME    win64       9.0.0.320924 (R2016a)    [1x1 matlab.unittest.TestResult]    a07f34c0-5653-4e01-b814-118fe30d3adf

性能测试框架为每个测试采集了 6 个样本测量值。

构造一个还会预备运行代码 3 次的计时试验。运行测试。

numWarmups = 3;
experiment = TimeExperiment.withFixedSampleSize(numSamples,'NumWarmups',numWarmups);
result = run(experiment,suite);
Running preallocationTest
..........
..........
....
Done preallocationTest
__________

查看第四个测试的测试活动。

result(4).TestActivity
ans = 

                Name                 Passed    Failed    Incomplete    MeasuredTime    Objective         Timestamp             Host        Platform           Version                      TestResult                          RunIdentifier            
    _____________________________    ______    ______    __________    ____________    _________    ____________________    ___________    ________    _____________________    ________________________________    ____________________________________

    preallocationTest/testForLoop    true      false     false         0.82972         warmup       29-Dec-2015 12:21:59    MY-HOSTNAME    win64       9.0.0.316358 (R2016a)    [1x1 matlab.unittest.TestResult]    37da664a-feba-4277-975f-3d71bcbac71a
    preallocationTest/testForLoop    true      false     false         0.85917         warmup       29-Dec-2015 12:22:00    MY-HOSTNAME    win64       9.0.0.316358 (R2016a)    [1x1 matlab.unittest.TestResult]    37da664a-feba-4277-975f-3d71bcbac71a
    preallocationTest/testForLoop    true      false     false         0.85857         warmup       29-Dec-2015 12:22:01    MY-HOSTNAME    win64       9.0.0.316358 (R2016a)    [1x1 matlab.unittest.TestResult]    37da664a-feba-4277-975f-3d71bcbac71a
    preallocationTest/testForLoop    true      false     false         0.85307         sample       29-Dec-2015 12:22:02    MY-HOSTNAME    win64       9.0.0.316358 (R2016a)    [1x1 matlab.unittest.TestResult]    37da664a-feba-4277-975f-3d71bcbac71a
    preallocationTest/testForLoop    true      false     false         0.86655         sample       29-Dec-2015 12:22:03    MY-HOSTNAME    win64       9.0.0.316358 (R2016a)    [1x1 matlab.unittest.TestResult]    37da664a-feba-4277-975f-3d71bcbac71a
    preallocationTest/testForLoop    true      false     false         0.81533         sample       29-Dec-2015 12:22:04    MY-HOSTNAME    win64       9.0.0.316358 (R2016a)    [1x1 matlab.unittest.TestResult]    37da664a-feba-4277-975f-3d71bcbac71a
    preallocationTest/testForLoop    true      false     false         0.88266         sample       29-Dec-2015 12:22:04    MY-HOSTNAME    win64       9.0.0.316358 (R2016a)    [1x1 matlab.unittest.TestResult]    37da664a-feba-4277-975f-3d71bcbac71a
    preallocationTest/testForLoop    true      false     false         0.94436         sample       29-Dec-2015 12:22:05    MY-HOSTNAME    win64       9.0.0.316358 (R2016a)    [1x1 matlab.unittest.TestResult]    37da664a-feba-4277-975f-3d71bcbac71a
    preallocationTest/testForLoop    true      false     false          1.0375         sample       29-Dec-2015 12:22:07    MY-HOSTNAME    win64       9.0.0.316358 (R2016a)    [1x1 matlab.unittest.TestResult]    37da664a-feba-4277-975f-3d71bcbac71a

对于每次测试,除了 6 个样本测量值外,性能测试框架还采集了 3 个预备测量值。

版本历史记录

在 R2016a 中推出