Main Content

スクリプトまたは関数を使用したパフォーマンスのテスト

この例では、4 通りのアプローチを使用して、ベクトルの事前割り当ての時間を計測するスクリプトベースまたは関数ベースのパフォーマンス テストを作成して実行する方法を説明します。

パフォーマンス テストの記述

現在のフォルダー内に preallocationTest.m という名前のファイルでパフォーマンス テストを作成します。この例では、次のスクリプトベースのテストまたは関数ベースのテストのいずれかを選択できます。この例の出力は、関数ベースのテストの出力です。スクリプトベースのテストを使用する場合は、テスト名が異なります。

スクリプトベースのパフォーマンス テスト関数ベースのパフォーマンス テスト
vectorSize = 1e7;

%% Ones Function
x = ones(1,vectorSize);

%% Indexing With Variable
id = 1:vectorSize;
x(id) = 1;

%% Indexing On LHS
x(1:vectorSize) = 1;

%% For Loop
for i=1:vectorSize
    x(i) = 1;
end
function tests = preallocationTest
tests = functiontests(localfunctions);
end

function testOnes(testCase)
vectorSize = getSize();
x = ones(1,vectorSize());
end

function testIndexingWithVariable(testCase)
vectorSize = getSize();
id = 1:vectorSize;
x(id) = 1;
end

function testIndexingOnLHS(testCase)
vectorSize = getSize();
x(1:vectorSize) = 1;
end

function testForLoop(testCase)
vectorSize = getSize();
for i=1:vectorSize
    x(i) = 1;
end
end

function vectorSize = getSize()
vectorSize = 1e7;
end

パフォーマンス テストの実行

関数 runperf を使用してパフォーマンス テストを実行します。

results = runperf("preallocationTest.m")
Running preallocationTest
.......... .......... .......... .......... .......
Done preallocationTest
__________


results = 

  1×4 TimeResult array with properties:

    Name
    Valid
    Samples
    TestActivity

Totals:
   4 Valid, 0 Invalid.
   8.7168 seconds testing time.

変数 results は 1 行 4 列の TimeResult 配列です。配列の各要素は、preallocationTest.m に定義されているテストの 1 つに対応しています。

テスト結果の表示

2 番目のテストの測定結果を表示します。結果は異なる場合があります。

results(2)
ans = 

  TimeResult with properties:

            Name: 'preallocationTest/testIndexingWithVariable'
           Valid: 1
         Samples: [4×7 table]
    TestActivity: [9×12 table]

Totals:
   1 Valid, 0 Invalid.
   0.87973 seconds testing time.

TestActivity プロパティのサイズで示されているとおり、パフォーマンス テスト フレームワークは 9 個の測定値を収集しました。この数の測定値には、コードをウォームアップするための 5 個の測定値が含まれています。Samples プロパティでは、ウォームアップ測定値は除外されます。

2 番目のテストのサンプル測定値を表示します。

results(2).Samples
ans =

  4×7 table

                       Name                       MeasuredTime         Timestamp             Host        Platform                 Version                             RunIdentifier            
    __________________________________________    ____________    ____________________    ___________    ________    __________________________________    ____________________________________

    preallocationTest/testIndexingWithVariable      0.096513      14-Oct-2022 14:04:00    MY-HOSTNAME     win64      9.14.0.2081372 (R2023a) Prerelease    7ff84b09-8a58-4961-bfcb-75c86f4a3ae1
    preallocationTest/testIndexingWithVariable      0.097008      14-Oct-2022 14:04:00    MY-HOSTNAME     win64      9.14.0.2081372 (R2023a) Prerelease    7ff84b09-8a58-4961-bfcb-75c86f4a3ae1
    preallocationTest/testIndexingWithVariable      0.096777      14-Oct-2022 14:04:00    MY-HOSTNAME     win64      9.14.0.2081372 (R2023a) Prerelease    7ff84b09-8a58-4961-bfcb-75c86f4a3ae1
    preallocationTest/testIndexingWithVariable      0.097157      14-Oct-2022 14:04:00    MY-HOSTNAME     win64      9.14.0.2081372 (R2023a) Prerelease    7ff84b09-8a58-4961-bfcb-75c86f4a3ae1

単一のテスト要素の統計の計算

2 番目のテストの平均測定時間を表示します。ウォームアップ実行で収集されたデータを除外するには、Samples プロパティの値を使用します。

sampleTimes = results(2).Samples.MeasuredTime;
meanTest2 = mean(sampleTimes)
meanTest2 =

    0.0969

すべてのテスト要素の統計の計算

各種事前割り当てメソッドを比較するために、results から要約統計量の table を作成します。この例では、関数 ones が 1 の値からなるベクトルを初期化する最も速い方法でした。パフォーマンス テスト フレームワークはこのテストの測定実行を 4 回行いました。

T = sampleSummary(results)
T =

  4×7 table

                       Name                       SampleSize      Mean      StandardDeviation      Min        Median       Max   
    __________________________________________    __________    ________    _________________    ________    ________    ________

    preallocationTest/testOnes                         4        0.016716       0.00018455        0.016515    0.016699    0.016952
    preallocationTest/testIndexingWithVariable         4        0.096864        0.0002817        0.096513    0.096892    0.097157
    preallocationTest/testIndexingOnLHS               15        0.024099        0.0025168        0.022721      0.0232    0.031685
    preallocationTest/testForLoop                      4         0.79044         0.016054         0.78203     0.78261     0.81452

統計目標値の変更とテストの再実行

時間実験を作成および実行することで、関数 runperf によって定義された統計目標値を変更します。時間実験の構築により、2 個のウォームアップ測定値を収集し、信頼度 98% の範囲で相対許容誤差 4% のサンプル平均値に達するまで、可変回数のテストを実行します。

テスト スイートを作成します。

suite = testsuite("preallocationTest");

指定した要件で時間実験を構築して、テスト スイートを実行します。

import matlab.perftest.TimeExperiment
experiment = TimeExperiment.limitingSamplingError("NumWarmups",2, ...
    "RelativeMarginOfError",0.04,"ConfidenceLevel",0.98);
resultsTE = run(experiment,suite);
Running preallocationTest
.......... .......... .......... ..........
Done preallocationTest
__________

すべてのテスト要素の要約統計量を計算します。

T1 = sampleSummary(resultsTE)
T1 =

  4×7 table

                       Name                       SampleSize      Mean      StandardDeviation      Min        Median       Max   
    __________________________________________    __________    ________    _________________    ________    ________    ________

    preallocationTest/testOnes                        16        0.017424         0.001223        0.016911    0.017056    0.021938
    preallocationTest/testIndexingWithVariable         8        0.099153        0.0039523        0.096619    0.097218     0.10736
    preallocationTest/testIndexingOnLHS                4        0.022985       0.00018664        0.022843    0.022921    0.023257
    preallocationTest/testForLoop                      4         0.80613         0.005993         0.79979     0.80591      0.8129

参考

| | |

関連するトピック