@hbaderts, , . , .
%% Below is to test read/write speed of struct, hashMap, and hashMap matrix assignment
numTrials= 10;
numKeys= 10000;
hw= zeros(1,numTrials);
hr= zeros(1,numTrials);
hmw= zeros(1,numTrials);
hmr= zeros(1,numTrials);
sw= zeros(1,numTrials);
sr= zeros(1,numTrials);
str= cell(1,numTrials);
for z= 1:numTrials
for e=1:numKeys
str{e}= strcat('adc',num2str(e));
end
% HashMap write
tic;
m= containers.Map();
for a=1:numKeys
m(str{a})=str{a};
end
hw(z)= toc;
% HashMap read
tic;
for b=1:numKeys
m(str{b});
end
hr(z)= toc;
% HashMap matrix write
tic;
keyval= cell(numKeys,1);
for a=1:numKeys
keyval{a}=str{a};
end
mm= containers.Map(keyval,keyval);
hmw(z)= toc;
% HashMap matrix read
tic;
for b=1:numKeys
mm(str{b});
end
hmr(z)= toc;
% Struct write
tic;
s= struct();
for c=1:numKeys
s.c.s.x.(str{c})= str{c};
end
sw(z)= toc;
% Struct read
tic;
for d=1:numKeys
s.c.s.x.(str{d});
end
sr(z)= toc;
end
fprintf('hashmap read time\n avg: %.3f seconds\n max: %.3f seconds\n',sum(hr)/numTrials,max(hr));
fprintf('hashmap write time\n avg: %.3f seconds\n max: %.3f seconds\n',sum(hw)/numTrials,max(hw));
fprintf('hashmap matrix read time\n avg: %.3f seconds\n max: %.3f seconds\n',sum(hmr)/numTrials,max(hmr));
fprintf('hashmap matrix write time\n avg: %.3f seconds\n max: %.3f seconds\n',sum(hmw)/numTrials,max(hmw));
fprintf('struct read time\n avg: %.3f seconds\n max: %.3f seconds\n',sum(sr)/numTrials,max(sr));
fprintf('struct write time\n avg: %.3f seconds\n max: %.3f seconds\n',sum(sw)/numTrials,max(sw));
.
hashmap read time.
avg: 0,301 .
max: 0,320 .
hashmap write time.
avg: 0.233 .
max: 0,247 .
hashmap matrix read time.
avg: 0,305 .
max: 0,323 .
hashmap matrix write time.
avg: 0,011 .
max: 0,016 .
struct read time.
avg: 0,059 .
max: 0,066 seconds.
struct write time.
avg: 0.040 seconds.
max: 0.046 seconds.
Let me know if this test is valid!
source
share