In your current working folder, create a class-based test, preallocationTest.m, that compares different methods of preallocation.
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
Create a test suite.
Construct a time experiment with a fixed number of sample measurements, and run the tests.
Running preallocationTest
..........
..........
..........
..........
Done preallocationTest
__________
result =
1×4 MeasurementResult array with properties:
Name
Valid
Samples
TestActivity
Totals:
4 Valid, 0 Invalid.