No BSD License  

Highlights from
Benchmark clear() behavior with OOP handle

Benchmark clear() behavior with OOP handle

by

Benjamin Heasly

 

Examine Matlab's "clear" behavior with different handle reference configurations.

handleClearBenchmarks
function handleClearBenchmarks
%Examine Matlab's "clear" behavior with different reference configurations
%
%   Create N handle objects, store 1-3 references to each.
%
%   Order of object creation, number of references, and configuration of
%   references all have drastic impact on "clear" time as function of
%   number of objects.  Times range from flat, to linear, to quadratic.
%
%   Note the log scale y-axis.
%
%   Using a simple handle class:
%
% % classdef simpleHandle < handle
% %     properties
% %         ref1;
% %         ref2;
% %     end
% %     methods
% %         function self = simpleHandel
% %         end
% %     end
% % end

% benjamin.heasly@gmail.com
%   Feb. 2009, Seattle, WA

funs = { ...
    @arrayAndDoubleLL, ...
    @doubleLL, ...
    @arrayAndBackwardLL, ...
    @backwardLL, ...
    @array, ...
    @forwardLL};

N = 100;

cla
for ii = 1:length(funs)
    clearTimes = zeros(1,N);
    for jj = 1:N
        clearTimes(jj) = feval(funs{ii}, jj);
    end
    line(1:N, clearTimes, 'Color', dec2bin(ii,3)=='1');
    strs{ii} = func2str(funs{ii});
end
xlabel('number of objects');
ylabel('time to "clear"');
set(gca, 'YScale', 'log');
legend(strs{:}, 'Location', 'NorthWest');

% object refers to previously created object
function clearTime = backwardLL(n)
r1 = simpleHandle;
for ii = 2:n
    r2 = simpleHandle;
    r2.ref1 = r1;
    r1 = r2;
end
tic
clear
clearTime = toc;

% array of objects
function clearTime = array(n)
for ii = n:-1:1
    refs(ii) = simpleHandle;
end
tic
clear
clearTime = toc;

% object refers to subsequently created object
function clearTime = forwardLL(n)
r1 = simpleHandle;
for ii = 2:n
    r2 = simpleHandle;
    r1.ref1 = r2;
    r1 = r2;
end
tic
clear
clearTime = toc;

% object refers to previously and subsequently created objects
function clearTime = doubleLL(n)
r1 = simpleHandle;
for ii = 2:n
    r2 = simpleHandle;
    r1.ref1 = r2;
    r2.ref2 = r1;
    r1 = r2;
end
tic
clear
clearTime = toc;

% array of objects, 
% each refers to previously created object
function clearTime = arrayAndBackwardLL(n)
refs(n) = simpleHandle;
for ii = n-1:-1:1
    refs(ii) = simpleHandle;
    refs(ii+1).ref1 = refs(ii);
end
tic
clear
clearTime = toc;

% array of objects, 
% each refers to previously and subsequently created objects
function clearTime = arrayAndDoubleLL(n)
refs(n) = simpleHandle;
for ii = n-1:-1:1
    refs(ii) = simpleHandle;
    refs(ii).ref1 = refs(ii+1);
    refs(ii+1).ref2 = refs(ii);
end
tic
clear
clearTime = toc;

Contact us