Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

comparisonPlot

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

创建绘图来比较基线和测量测试结果

自 R2019b 起

说明

示例

comparisonPlot(Baseline,Measurement) 创建绘图,用于直观地比较数组 BaselineMeasurement 中的每个 TimeResult 对象。该比较基于最小样本测量时间。

示例

comparisonPlot(Baseline,Measurement,stat) 指定应用于 BaselineMeasurement 中每个 TimeResult 对象的样本测量时间的统计数据。

示例

comparisonPlot(___,Name,Value) 创建一个比较图,并通过一个或多个 Name,Value 对组参数指定其他选项。例如,comparisonPlot(Baseline,Measurement,'Scale','linear') 使用带线性刻度的 x 轴和 y 轴创建一个绘图。

示例

cp = comparisonPlot(___) 返回 ComparisonPlot 对象,指定为 matlab.unittest.measurement.chart.ComparisonPlot 类的实例。创建比较图后,可使用 cp 修改其属性。

输入参数

全部展开

对测试套件运行主计时试验的结果,指定为 TimeResult 数组。Baseline 的长度必须与 Measurement 的长度相同。

对测试套件运行次计时试验的结果,指定为 TimeResult 数组。Measurement 的长度必须与 Baseline 的长度相同。

应用于 BaselineMeasurement 中每个 TimeResult 对象的样本测量时间的统计数据,指定为 'min''max''mean''median'

名称-值参数

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

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

示例: cp = comparisonPlot(Baseline,Measurement,'SimilarityTolerance',0.05,'Scale','linear')

ComparisonPlot 对象的 x 和 y 轴的刻度,指定为 'log''linear'

一对相似性能测试的统计量比率相对于 1 的允许偏差,指定为 01 之间的数值。

SimilarityTolerance 指定比较图中阴影区域的边界。该区域内的数据点表示相似的 BaselineMeasurement 项。

要在其中绘图的父容器,指定为使用 figureuifigure 函数创建的 Figure 对象、使用 uipanel 函数创建的 Panel 对象或使用 uitab 函数创建的 Tab 对象。

示例

全部展开

可视化两种排序算法(气泡排序和合并排序)的计算复杂度,这两种算法按升序对列表元素进行排序。气泡排序是一种简单的排序算法,它重复遍历列表,比较相邻的一对元素,并在元素顺序错误时交换元素。合并排序是一种“分而治之”的算法,它将排序的子列表合并为新排序列表,利用了这一机制的便捷性。

在当前文件夹中,将以下代码保存在 bubbleSort.m 中。

function y = bubbleSort(x)
% Sorting algorithm with O(n^2) complexity
n = length(x);
swapped = true;

while swapped
    swapped = false;
    for i = 2:n
        if x(i-1) > x(i)
            temp = x(i-1);
            x(i-1) = x(i);
            x(i) = temp;
            swapped = true;
        end
    end
end

y=x;
end

将以下代码保存在 mergeSort.m 中。

function y = mergeSort(x)
% Sorting algorithm with O(n*logn) complexity
y = x; % A list of one element is considered sorted

if length(x) > 1
    mid = floor(length(x)/2);
    L = x(1:mid);
    R = x((mid+1):end);
    
    % Sort left and right sublists recursively
    L = mergeSort(L);
    R = mergeSort(R);
    
    % Merge the sorted left (L) and right (R) sublists
    i = 1;
    j = 1;
    k = 1;
    while i <= length(L) && j <= length(R)
        if L(i) < R(j)
            y(k) = L(i);
            i = i + 1;
        else
            y(k) = R(j);
            j = j + 1;
        end
        k = k + 1;
    end
    
    % At this point, either L or R is empty
    while i <= length(L)
        y(k) = L(i);
        i = i + 1;
        k = k + 1;
    end
    
    while j <= length(R)
        y(k) = R(j);
        j = j + 1;
        k = k + 1;
    end
end

end

创建以下参数化测试类 TestSort,它比较气泡排序算法和合并排序算法的性能。len 属性包含您要测试的列表元素的数量。

classdef TestSort < matlab.perftest.TestCase
    properties
        Data
        SortedData
    end
    
    properties(ClassSetupParameter)
        % Create 25 logarithmically spaced values between 10^2 and 10^4
        len = num2cell(round(logspace(2,4,25)));
    end
    
    methods(TestClassSetup)
        function ClassSetup(testCase,len)
            orig = rng;
            testCase.addTeardown(@rng,orig)
            rng('default')
            testCase.Data = rand(1,len);
            testCase.SortedData = sort(testCase.Data);
        end
    end
    
    methods(Test)
        function testBubbleSort(testCase)
            while testCase.keepMeasuring
                y = bubbleSort(testCase.Data);
            end
            testCase.verifyEqual(y,testCase.SortedData);
        end
        
        function testMergeSort(testCase)
            while testCase.keepMeasuring
                y = mergeSort(testCase.Data);
            end
            testCase.verifyEqual(y,testCase.SortedData);
        end
        
    end
end

对所有对应于 'testBubbleSort' 方法的测试元素运行性能测试,并将结果保存在 Baseline 数组中。您的结果可能与所示的结果不同。

Baseline = runperf('TestSort','ProcedureName','testBubbleSort');
Running TestSort
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .....
Done TestSort
__________

对所有对应于 'testMergeSort' 方法的元素运行性能测试,并将结果保存在 Measurement 数组中。

Measurement = runperf('TestSort','ProcedureName','testMergeSort');
Running TestSort
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
Done TestSort
__________

对于每组 BaselineMeasurement 对象,直观地比较其 Samples 表中 MeasuredTime 列的最小值。

comparisonPlot(Baseline,Measurement);

Comparison plot based on the minimum of sample measurement times

在此比较图中,大部分数据点为蓝色,因为它们位于阴影区域(表示相似)的下方。此结果表明,对于大多数测试用例来说,合并排序性能更优。但是,对于足够小的列表,气泡排序相比合并排序表现相当或更好,如图中橙色和灰色点所示。

作为比较汇总,该图报告使用合并排序可使性能提升 78%。该值是对应于所有数据点的提升百分比的几何均值。如果比较图包含无效数据点,则不会生成比较汇总。

您可以用鼠标在任何数据点上点击或悬停,进一步了解正在比较的时间测量结果。

Comparison plot based on the minimum of sample measurement times. A data tip displays detailed information about one of the points.

要研究不同列表长度下的最差情形排序算法性能,请根据样本测量时间的最大值创建比较图。

comparisonPlot(Baseline,Measurement,'max');

Comparison plot based on the maximum of sample measurement times

在比较样本测量时间的最大值时,将 SimilarityTolerance 减少到 0.01。在 cp 中返回 ComparisonPlot 对象,以便您以后修改其属性。

cp = comparisonPlot(Baseline,Measurement,'max','SimilarityTolerance',0.01);

Comparison plot based on the maximum of sample measurement times. Similarity tolerance is 0.01.

版本历史记录

在 R2019b 中推出