The difference between a container map and a struct in matlab

前端 未结 3 635
醉酒成梦
醉酒成梦 2020-12-06 06:48

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

From what I\'ve looked, it seems that the advantages of a container map is th

相关标签:
3条回答
  • 2020-12-06 06:56

    Functionality wise, the containers.Map is almost a generalization of struct, i.e.:

    • Keys of containers.Map are not limited to be valid identifiers strings.
    • Optionally, type checking on containers.Map values can be enforced.

    One notable difference is that instances of containers.Map are passed by reference, whereas struct objects are passed by value.

    Design wise, containers.Map were introduced to fill the gap with java.util.Map (see Using Java Collections in Matlab). This somehow explains the different pass-by convention, because all instances of Java Object are passed by reference in Matlab (see Pass Data to Java Methods).

    History wise, containers.Map came in R2008b, i.e. after struct, which came before R2006a.

    0 讨论(0)
  • 2020-12-06 06:58

    The concepts behind a container map and a struct are quite different:

    A container map is used to create a mapping or indexing. For example if you have a matrix A and want to index the element in the second row and second column, you will index it using A(2,2). For numbers, matrices and so on, where you want to index a specific row number, this is convenient. Assume however, you have a situation as shown in the following example by Mathworks:

    Here you have an array of values, which you could index as value(1) for January, and so on. However this is not very legible. It would be much more convenient if you could index it with value('Jan'). This is exactly what container maps offer. As @marsei remarked in his comment, container maps are a Java-based, unordered, construct, that uses hash-tables for indexing.

    A struct is a different construct, which is C-based and ordered (thanks for the insignt @marsei). The main use of struct is to help you store data in a more logical way. For example when using images, you often have two variables: one for the image data, and one for the color map. Without structs, you need to keep those two separate variables in the workspace. For multiple images this gets very confusing (e.g. using names like img0_data, img0_map and so on). A struct helps you organize this in a simple way: A struct called img0 with the fields data and map.

    In the context of dictionaries, both constructs are more or less equivalent, though structs usually seem to be faster than container maps. Plus, as already mentioned in the question, the key of a container map can be any single value, while for structs it has to be a string.

    0 讨论(0)
  • 2020-12-06 07:08

    After reading @hbaderts's answer, it got me wondering what the speed difference actually is. Hence, I went ahead and tried some benchmark test.

    %%  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));
    

    Result:.

    hashmap read time.

    avg: 0.301 seconds.

    max: 0.320 seconds.

    hashmap write time.

    avg: 0.233 seconds.

    max: 0.247 seconds.

    hashmap matrix read time.

    avg: 0.305 seconds.

    max: 0.323 seconds.

    hashmap matrix write time.

    avg: 0.011 seconds.

    max: 0.016 seconds.

    struct read time.

    avg: 0.059 seconds.

    max: 0.066 seconds.

    struct write time.

    avg: 0.040 seconds.

    max: 0.046 seconds.

    Let me know if this test looks valid!

    0 讨论(0)
提交回复
热议问题