Skip to content

Commit

Permalink
Re #1786 optimized for indices access fast_map class. Working
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Feb 5, 2025
1 parent b056864 commit c3fbd9a
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 62 deletions.
74 changes: 74 additions & 0 deletions _test/test_utilities_herbert/fast_map_vs_map_performance.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
n_keys = 500;
base_key = 10+round(rand(1,10*n_keys)*(10*n_keys-1));
base_key = unique(base_key);


base_key = base_key(1:n_keys);
keysUint = uint32(base_key);
mm = min_max(keysUint)

n_operations= 100000;


test_keys = repmat(base_key,1,n_operations);
n_idx = n_keys*n_operations;
rnd_idx = randperm(n_idx);
test_keys= test_keys(rnd_idx);



map_stor1 = containers.Map('KeyType',class(keysUint),'ValueType','double');


% check presence and add values

tv = tic;
for i=1:n_idx
[idx1,map_stor1] = add_to_map(map_stor1,test_keys(i));
end
tv = toc(tv);
fprintf('Find & Add keys to UINT map takes %gsec\n',tv)
fm = fast_map();
tv = tic;
for i=1:n_idx
if fm.isKey(test_keys(i))
idx1 = fm.get(test_keys(i));
else
fm = fm.add(test_keys(i),i);
end
end
tv = toc(tv);
fprintf('Find & Add keys FAST MAP map takes %gsec\n',tv)
tv = tic;
for i=1:n_idx
idx1 = map_stor1(test_keys(i));
end
tv = toc(tv);
fprintf('Find keys in UINT map takes %gsec\n',tv)
tv = tic;
for i=1:n_idx
idx1 = fm.get(test_keys(i));
end
tv = toc(tv);
fprintf('Find keys in FAST MAP map takes %gsec\n',tv)

fm.optimized = true;
tv = tic;
for i=1:n_idx
idx1 = fm.get(test_keys(i));
end
tv = toc(tv);
fprintf('Find keys in FAST MAP Opt map takes %gsec\n',tv)



function [idx,map] = add_to_map(map,value)
is = map.isKey(value);
if is
idx = map(value);
else
idx = map.length;
map(value) = idx+1;
end
end

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
n_keys = 10;
n_keys = 100;
n_operations= 10000;
base_val = rand(1,n_keys);

Expand Down
57 changes: 57 additions & 0 deletions _test/test_utilities_herbert/test_fast_map.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
classdef test_fast_map < TestCase
properties
end
methods
function obj = test_fast_map(varargin)
if nargin<1
name = 'test_fast_map';
else
name = varargin{1};
end
obj = obj@TestCase(name);
end
%------------------------------------------------------------------
function test_optimization(~)
n_keys = 100;
base_key = 10+round(rand(1,10*n_keys)*(10*n_keys-1));
base_key = unique(base_key);
n_keys = numel(base_key);
val = 1:n_keys;

fm = fast_map(base_key,val);
fmop = fm;
fmop.optimized = true;
for i=1:n_keys
assertEqual(fm.get(base_key(i)),fmop.get(base_key(i)));
end

end
%------------------------------------------------------------------
function test_fast_map_accepts_addition(~)
keys = 10:-1:1;

fm = fast_map();
for i=1:10
fm = fm.add(keys(i),i);
end
assertEqual(fm.keys,uint32(10:-1:1));
assertEqual(fm.values,1:10);
end
%
function test_fast_map_construction(~)
val = 10:-1:1;
fm = fast_map(1:10,val );
assertEqual(fm.keys,uint32(1:10));
assertEqual(fm.values,val);

for i=1:numel(val)
assertEqual(fm.get(i),val(i));
end
end
function test_fast_map_empty_constructor(~)
fm = fast_map();
assertTrue(isempty(fm.keys));
assertTrue(isempty(fm.values));
end
end
end
Loading

0 comments on commit c3fbd9a

Please sign in to comment.