Just to add to the previous answers (could not comment on the other good answers) : a struct lookup like this:
s={};
s.abc = 1; %insert a value in the struct, abc is your lookup hash
out = s.abc; % read it back
The actual read back of the value is about 10 times faster with a struct than using a container. Full test code as follows if interesting
function s=test_struct_lookup_hash_speed
%% test how a struct lookup speed works vs container, interesting
s = {}; % hash table
v = {}; % reverselookup table for testing
nHashes = 1E4; % vary this to see if read speed varies by size (NOT REALLY)
nReads = 1E6;
fprintf('Generating hash struct of %i entries\n', nHashes);
tic
for i = 1:nHashes
hashStr = md5fieldname(randi(1E8));
s.(hashStr) = i;
v{end+1} = hashStr; %reverselookup
end
toc
fprintf('Actual HashTable length (due to non unique hashes?): %i, and length of reverse table: %i\n',length(fieldnames(s)), length(v) );
fprintf('Reading %i times from a random selection from the %i hashes\n', nReads, nHashes);
vSubset = [ v(randi(nHashes,1,3)) ];
for i = 1:length(vSubset)
hashStr = vSubset{i};
% measure read speed only
tic
for j = 1:nReads
val = s.(hashStr);
end
toc
end
%% test CONTAINERS
fprintf('Testing Containers\n');
c = containers.Map;
fprintf('Generating hash struct of %i entries\n', nHashes);
tic
for i = 1:nHashes
hashStr = md5fieldname(randi(1E8));
c(hashStr) = i;
v{end+1} = hashStr; %reverselookup
end
toc
fprintf('Reading %i times from a random selection from the %i hashes\n', nReads, nHashes);
vSubset = [ v(randi(nHashes,1,3)) ];
for i = 1:length(vSubset)
hashStr = vSubset{i};
% measure read speed only
tic
for j = 1:nReads
val = c(hashStr);
end
toc
end
%% Get a valid fieldname (has to start with letter)
function h=md5fieldname(inp)
h = ['m' hash(inp,'md5')];
end
end