Re: vector::push_back performance

From: Phlip (phlip_cpp_at_yahoo.com)
Date: 09/29/04

  • Next message: Keith Thompson: "Re: #include optimization"
    Date: Tue, 28 Sep 2004 23:56:18 GMT
    
    

    Antonios Christofides wrote:

    > As I read in the archives, the performance problem caused by memory
    > reallocations during vector::push_back is a common one. My first C++
    > program is suffering from it: 300 thousand push_backs result,
    > according to the profiler, in 20 reallocations; these 20 reallocations
    > account for 3.6 seconds (Celeron 1.3G), which is 40% of total
    > execution time.

    Do you think you could go to an algorithm where you push less back?

    > What I don't understand: why is the reallocation code so complex? I
    > studied the library source and I have a hard time understanding it,
    > but it seems to be copying the vector item by item in each
    > reallocation. Why wouldn't a "realloc" suffice?

    Read Herb Sutter's way-cool GOTW series, and his books /Exceptional C++/. He
    impugns the container class of choice should usually be std::deque<>, not
    std::vector<>. It frags not memory like std::list<>, and it's optimal to
    push things to both the beginning and end.

    -- 
      Phlip
      http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    

  • Next message: Keith Thompson: "Re: #include optimization"

    Relevant Pages

    • Re: Aspiring highest-order programmer
      ... An NP algorithm, as Ian reminded us, is ... one whose execution time formula is not a polynomial. ... >>his own goddamn programming and is man enough to admit error) it was ... > reason to suspect that no such algorithm exists. ...
      (comp.programming)
    • compare 2 samples
      ... I've set a test machine up so that it switches between algorithm implementations and keeps simple timing records for each alg. ... I would like to compare the 2 results and state with some confidence that there really is a difference. ... When I first looked at the 2 results, I assumed that it was a simple one sided hypothesis test. ... Assuming that the probability distribution for execution times on a highly concurrent server is normal, we would formulate a null hypothesis that the execution time for P is greater than or equal to the execution time of Q. The alternative hypothesis being that the execution time of P is less than the execution time for Q. ...
      (sci.stat.math)
    • Re: Aspiring highest-order programmer
      ... [big snip] ... >able to tell, from a practical program's graph of execution time, the ... >derivative of its execution time formula by feel. ... >> algorithm is still reasonable for pretty large inputs. ...
      (comp.programming)
    • Re: Fastcode UpperCase B&V 2.6
      ... that execution time of a significant part of algorithm ... interruptions which wash away results of measurements even more. ... don't forget to count a delay after use MMX, ...
      (borland.public.delphi.language.basm)
    • Re: a basic question about execution speed
      ... It depends on the time complexity of the algorithm. ... the same hardware resources), must the execution time of each run be ...
      (comp.programming)

    Loading