Difference between container map and structure in Matlab

I would like to understand the difference between container map and structure in matlab.

From what I looked, it seems that the advantages of the container map is that it can use any number or string as a key, while the struct field can only accept strings that are legal variable names (for example, t accept mystruct.('123string'))

Are there any other advantages to using a container map over a structure?

thanks

+7
source share
3 answers

The concepts underlying the container map and structure are completely different:

. , A , , A(2,2). , .., , . , , , Mathworks:

example

, value(1) . . , value('Jan'). , . @marsei , - Java, - .

- , C ( insignt @marsei). struct - . , : . . (, , img0_data, img0_map ..). : img0 data map.

, . , , key , .

+8

, containers.Map struct, :

  • containers.Map .
  • , containers.Map .

, containers.Map , struct .

containers.Map , java.util.Map (. Java Matlab). - , Java Object Matlab (. Java).

, containers.Map R2008b, struct, R2006a.

+8

@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!

+2
source

Source: https://habr.com/ru/post/1624099/


All Articles