-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re #1786 optimized for indices access fast_map class. Working
- Loading branch information
Showing
5 changed files
with
295 additions
and
62 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
_test/test_utilities_herbert/fast_map_vs_map_performance.m
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
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 | ||
|
2 changes: 1 addition & 1 deletion
2
...t_sqw_class/ismember_vs_map_performance.m → ...ies_herbert/ismember_vs_map_performance.m
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
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); | ||
|
||
|
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
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 |
Oops, something went wrong.