Test block. However, the measured time includes the time to perform the verifications.
Best practice is to measure a more accurate performance boundary.
Create a performance test in a file, fprintfTest.m, in your current working folder. This
test is similar to the regression test with the following modifications:
- The test inherits from matlab.perftest.TestCase instead of
matlab.unittest.TestCase. - The test calls the startMeasuring and stopMeasuring methods to create a
boundary around the fprintf function call.
classdef fprintfTest < matlab.perftest.TestCase
properties
file
fid
end
methods(TestMethodSetup)
function openFile(testCase)
testCase.file = tempname;
testCase.fid = fopen(testCase.file,'w');
testCase.assertNotEqual(testCase.fid,-1,'IO Problem')
testCase.addTeardown(@delete,testCase.file);
testCase.addTeardown(@fclose,testCase.fid);
end
end
methods(Test)
function testPrintingToFile(testCase)
textToWrite = repmat('abcdef',1,5000000);
testCase.startMeasuring();
fprintf(testCase.fid,'%s',textToWrite);
testCase.stopMeasuring();
testCase.verifyEqual(fileread(testCase.file),textToWrite)
end
function testBytesToFile(testCase)
textToWrite = repmat('tests_',1,5000000);
testCase.startMeasuring();
nbytes = fprintf(testCase.fid,'%s',textToWrite);
testCase.stopMeasuring();
33 Unit Testing