What is the preferred/idiomatic way to insert into a map?

前端 未结 9 1475
感动是毒
感动是毒 2020-11-28 18:45

I have identified four different ways of inserting elements into a std::map:

std::map function;

function[0] = 42;
function.inse         


        
9条回答
  •  孤独总比滥情好
    2020-11-28 19:13

    I have been running some time comparisons between the abovementioned versions:

    function[0] = 42;
    function.insert(std::map::value_type(0, 42));
    function.insert(std::pair(0, 42));
    function.insert(std::make_pair(0, 42));
    

    Turns out that time differences between the insert versions are tiny.

    #include 
    #include 
    #include 
    using namespace boost::posix_time;
    class Widget {
    public:
        Widget() {
            m_vec.resize(100);
            for(unsigned long it = 0; it < 100;it++) {
                m_vec[it] = 1.0;
            }
        }
        Widget(double el)   {
            m_vec.resize(100);
            for(unsigned long it = 0; it < 100;it++) {
                m_vec[it] = el;
            }
        }
    private:
        std::vector m_vec;
    };
    
    
    int main(int argc, char* argv[]) {
    
    
    
        std::map map_W;
        ptime t1 = boost::posix_time::microsec_clock::local_time();    
        for(int it = 0; it < 10000;it++) {
            map_W.insert(std::pair(it,Widget(2.0)));
        }
        ptime t2 = boost::posix_time::microsec_clock::local_time();
        time_duration diff = t2 - t1;
        std::cout << diff.total_milliseconds() << std::endl;
    
        std::map map_W_2;
        ptime t1_2 = boost::posix_time::microsec_clock::local_time();    
        for(int it = 0; it < 10000;it++) {
            map_W_2.insert(std::make_pair(it,Widget(2.0)));
        }
        ptime t2_2 = boost::posix_time::microsec_clock::local_time();
        time_duration diff_2 = t2_2 - t1_2;
        std::cout << diff_2.total_milliseconds() << std::endl;
    
        std::map map_W_3;
        ptime t1_3 = boost::posix_time::microsec_clock::local_time();    
        for(int it = 0; it < 10000;it++) {
            map_W_3[it] = Widget(2.0);
        }
        ptime t2_3 = boost::posix_time::microsec_clock::local_time();
        time_duration diff_3 = t2_3 - t1_3;
        std::cout << diff_3.total_milliseconds() << std::endl;
    
        std::map map_W_0;
        ptime t1_0 = boost::posix_time::microsec_clock::local_time();    
        for(int it = 0; it < 10000;it++) {
            map_W_0.insert(std::map::value_type(it,Widget(2.0)));
        }
        ptime t2_0 = boost::posix_time::microsec_clock::local_time();
        time_duration diff_0 = t2_0 - t1_0;
        std::cout << diff_0.total_milliseconds() << std::endl;
    
        system("pause");
    }
    

    This gives respectively for the versions (I ran the file 3 times, hence the 3 consecutive time differences for each):

    map_W.insert(std::pair(it,Widget(2.0)));
    

    2198 ms, 2078 ms, 2072 ms

    map_W_2.insert(std::make_pair(it,Widget(2.0)));
    

    2290 ms, 2037 ms, 2046 ms

     map_W_3[it] = Widget(2.0);
    

    2592 ms, 2278 ms, 2296 ms

     map_W_0.insert(std::map::value_type(it,Widget(2.0)));
    

    2234 ms, 2031 ms, 2027 ms

    Hence, results between different insert versions can be neglected (didn't perform a hypothesis test though)!

    The map_W_3[it] = Widget(2.0); version takes about 10-15 % more time for this example due to an initialization with the default constructor for Widget.

提交回复
热议问题