matlab.perftest.TestCase 类
命名空间: matlab.perftest
超类: matlab.unittest.TestCase
用于使用性能测试框架编写测试的类
描述
matlab.perftest.TestCase
类可用于编写基于类的性能测试,并定义将测量限制到特定代码段的边界。由于 matlab.perftest.TestCase
类是从 matlab.unittest.TestCase
类派生的,因此您的测试可以使用单元测试框架的功能。因此,您可以在性能测试中执行鉴定,以确保在测量代码性能时功能行为正确。有关创建和运行性能测试的详细信息,请参阅性能测试框架概述。
在运行测试时,性能测试框架会自动创建 matlab.perftest.TestCase
实例。
matlab.perftest.TestCase
类是一个 handle
类。
方法
公共方法
除了这些方法之外,matlab.perftest.TestCase
类还从 matlab.unittest.TestCase
类及其超类继承方法。
keepMeasuring | 使用自动循环测量代码 |
startMeasuring | 指定测量开始边界 |
stopMeasuring | 指定测量结束边界 |
示例
创建基于类的性能测试
通过创建一个从 matlab.perftest.TestCase
派生的测试类来比较各种预分配方法的性能。
在当前文件夹的名为 preallocationTest.m
的文件中创建 preallocationTest
测试类。该类包含四个 Test
方法,对应于创建由 1 组成的向量的不同方法。当您使用 runperf
函数运行上述任一方法时,该函数会测量在该方法内运行代码所花费的时间。
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
对名称中包含 "Indexing"
的所有测试运行性能测试。您的结果可能有所不同,而且如果 runperf
不满足统计目标,您可能会看到一条警告消息。
results = runperf("preallocationTest","Name","*Indexing*")
Running preallocationTest .......... .......... .......... .. Done preallocationTest __________
results = 1×2 TimeResult array with properties: Name Valid Samples TestActivity Totals: 2 Valid, 0 Invalid. 3.011 seconds testing time.
要比较预分配方法,请基于 results
创建一个汇总统计量表。在此示例中,testIndexingOnLHS
方法是将向量值全部初始化为 1 的更快方法。
T = sampleSummary(results)
T=2×7 table
Name SampleSize Mean StandardDeviation Min Median Max
__________________________________________ __________ ________ _________________ ________ ________ ________
preallocationTest/testIndexingWithVariable 17 0.1223 0.014378 0.10003 0.12055 0.15075
preallocationTest/testIndexingOnLHS 5 0.027557 0.0013247 0.026187 0.027489 0.029403
可视化比较排序算法性能
可视化两种排序算法(气泡排序和合并排序)的计算复杂度,这两种算法按升序对列表元素进行排序。气泡排序是一种简单的排序算法,它重复遍历列表,比较相邻的一对元素,并在元素顺序错误时交换元素。合并排序是一种“分而治之”的算法,它将排序的子列表合并为新排序列表,利用了这一机制的便捷性。
在当前文件夹中名为 bubbleSort.m
的文件中,创建 bubbleSort
函数,该函数实现气泡排序算法。
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
的文件中,创建 mergeSort
函数,该函数实现合并排序算法。
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
在当前文件夹中名为 SortTest.m
的文件中,创建 SortTest
参数化测试类,该类比较气泡排序和合并排序算法的性能。该类的 len
属性包含要用于测试的列表元素的数量。
classdef SortTest < 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("SortTest","ProcedureName","testBubbleSort");
Running SortTest .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .. Done SortTest __________
对所有对应于 testMergeSort
方法的测试运行性能测试,并将结果保存在 measurement
数组中。
measurement = runperf("SortTest","ProcedureName","testMergeSort");
Running SortTest .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... ..... Done SortTest __________
对于每组 baseline
和 measurement
对象,直观地比较其 Samples
表中对应 MeasuredTime
列的最小值。在此比较图中,大部分数据点为蓝色,因为它们位于阴影区域(表示相似)的下方。此结果表明,对于大多数测试来说,合并排序性能更优。但是,对于足够小的列表,气泡排序相比合并排序表现相当或更好,如图中橙色和灰色点所示。作为比较汇总,该图报告合并排序比气泡排序快 80%。该值是对应于所有数据点的提升百分比的几何均值。
cp = comparisonPlot(baseline,measurement);
您可以用鼠标点击或指向任何数据点,进一步了解正在比较的时间测量结果。
要研究不同列表长度下的最差情形排序算法性能,请根据样本测量时间的最大值创建比较图。
cp = comparisonPlot(baseline,measurement,"max");
在比较样本测量时间的最大值时,将相似性容差减少到 0.01
。
cp = comparisonPlot(baseline,measurement,"max","SimilarityTolerance",0.01);
版本历史记录
在 R2016a 中推出
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)