Created
July 2, 2014 14:09
-
-
Save jmc734/40b4a1f13b11d2577516 to your computer and use it in GitHub Desktop.
Compare overall insert and get times for containers.Map and java.util.HashMap in MATLAB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clear; | |
clc; | |
import containers.Map; | |
import java.util.*; | |
MM = 10000; | |
NN = round(logspace(1,4)); | |
times = zeros(length(MM),length(NN),2); | |
for x = 1:length(MM) | |
M = MM(x); | |
for y = 1:length(NN) | |
N = NN(y); | |
I = 1:M; | |
J = 1:N; | |
keys = cell(N,1); | |
for j = J | |
keys{j} = num2str(j); | |
end | |
order = randi(N, M, 2); | |
start = clock(); | |
m = Map(); | |
for i = I | |
j = order(i,1); | |
m(keys{j}) = j; | |
end | |
for i = I | |
j = order(i,1); | |
m(keys{j}); | |
end | |
finish = clock(); | |
fprintf('%-30s %.2fs\n', sprintf('containers.Map (%d,%d):', M, N), etime(finish, start)); | |
times(x,y,1) = etime(finish, start); | |
clear m finish start; | |
start = clock(); | |
m = java.util.HashMap(); | |
for i = I | |
j = order(i,1); | |
m.put(keys{j}, j); | |
end | |
for i = I | |
j = order(i,1); | |
m.get(keys{j}); | |
end | |
finish = clock(); | |
fprintf('%-30s %.2fs\n', sprintf('java.util.HashMap (%d,%d):', M, N), etime(finish, start)); | |
times(x,y,2) = etime(finish, start); | |
clear m finish start; | |
end | |
end | |
figure; | |
subplot(2,1,1); | |
semilogx(NN, times(1,:,1), NN, times(1,:,2)); | |
xlabel('Number of Elements'); | |
ylabel('Time [s]'); | |
legend('containers.Map', 'java.util.HashMap'); | |
title('Execution time for Insert and Get'); | |
subplot(2,1,2); | |
semilogx(NN, 20*log10(times(1,:,1)./times(1,:,2))); | |
xlabel('Number of Elements'); | |
ylabel('Ratio [dB]'); | |
title('Ratio of containers.Map time to java.util.HashMap time'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment