
200 Accelerating MATLAB
®
Performance
function value = getSize(obj)
value = obj.Width * obj.Height;
end
end
end
function value = getSizeExt(width, height) % regular m-function
value = width * height;
end
C = TestClass2;
tic, for idx=1:1000; v = C.getSize(); end, toc
⇨ Elapsed time is 0.024017 seconds.
% getSize(C) is 1.3x faster than C.getSize()
tic, for idx=1:1000; v = getSize(C); end, toc
⇨ Elapsed time is 0.018949 seconds.
% getSizeExt() is even faster (7x)
tic, for idx=1:1000; v = getSizeExt(C.Width,C.Height); end, toc
⇨ Elapsed time is 0.003361 seconds.
% Avoiding the function call is the fastest alternative (1100x faster) ...