Why does compiling over 100,000 lines of std::vector::push_back take a long time?

前端 未结 3 1977
天命终不由人
天命终不由人 2021-02-01 06:01

I\'m compiling a C++ library which defines a single function that randomly samples from a set of data points. The data points are stored in a std::vector. There a

3条回答
  •  臣服心动
    2021-02-01 06:48

    This question was completely answered by the great answer from osgx.

    Maybe one additional aspect: push_back() vs initialization list

    When running the above test with 100000 push_backs, I get the following result with a gcc 4.4.6 on a Debian 6.0.6 system:

    $ time g++ -std=c++0x -ftime-report ./pb100k.cc 
    
    Execution times (seconds)
     garbage collection    :   0.55 ( 1%) usr   0.00 ( 0%) sys   0.55 ( 1%) wall       0 kB ( 0%) ggc
     ...
     reload                :  33.95 (58%) usr   0.13 ( 6%) sys  34.14 (56%) wall   65723 kB ( 9%) ggc
     thread pro- & epilogue:   0.66 ( 1%) usr   0.00 ( 0%) sys   0.66 ( 1%) wall      84 kB ( 0%) ggc
     final                 :   1.82 ( 3%) usr   0.01 ( 0%) sys   1.81 ( 3%) wall      21 kB ( 0%) ggc
     TOTAL                 :  58.65             2.13            60.92             737584 kB
    
    real    1m2.804s
    user    1m0.348s
    sys     0m2.328s
    

    When using an initialization list, it is much faster:

    $ cat pbi100k.cc
    #include 
    using namespace std;
    
    int main()
    {
       vector d {
       0.190987822870774,
       /* 100000 lines with doubles generated with:
              perl -e 'print(rand(10),",\n") for 1..100000'
       */
       7.45608614801021};
    
      return d.size();
    }
    
    $ time g++ -std=c++0x -ftime-report ./pbi100k.cc 
    
    Execution times (seconds)
     callgraph construction:   0.02 ( 2%) usr   0.00 ( 0%) sys   0.02 ( 1%) wall      25 kB ( 0%) ggc
     preprocessing         :   0.72 (59%) usr   0.06 (25%) sys   0.80 (54%) wall    8004 kB (12%) ggc
     parser                :   0.24 (20%) usr   0.12 (50%) sys   0.36 (24%) wall   43185 kB (65%) ggc
     name lookup           :   0.01 ( 1%) usr   0.05 (21%) sys   0.03 ( 2%) wall    1447 kB ( 2%) ggc
     tree gimplify         :   0.01 ( 1%) usr   0.00 ( 0%) sys   0.02 ( 1%) wall     277 kB ( 0%) ggc
     tree find ref. vars   :   0.01 ( 1%) usr   0.00 ( 0%) sys   0.01 ( 1%) wall      15 kB ( 0%) ggc
     varconst              :   0.19 (15%) usr   0.01 ( 4%) sys   0.20 (14%) wall   11288 kB (17%) ggc
     integrated RA         :   0.02 ( 2%) usr   0.00 ( 0%) sys   0.02 ( 1%) wall      74 kB ( 0%) ggc
     reload                :   0.01 ( 1%) usr   0.00 ( 0%) sys   0.01 ( 1%) wall      61 kB ( 0%) ggc
     TOTAL                 :   1.23             0.24             1.48              66378 kB
    
    real    0m1.701s
    user    0m1.416s
    sys     0m0.276s
    

    This is about 30+ times faster!

提交回复
热议问题