Re: vector::push_back performance
From: Phlip (phlip_cpp_at_yahoo.com)
Date: 09/29/04
- Previous message: Andre Kostur: "Re: #include optimization"
- In reply to: Antonios Christofides: "vector::push_back performance"
- Next in thread: Andrew Koenig: "Re: vector::push_back performance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: Andre Kostur: "Re: #include optimization"
- In reply to: Antonios Christofides: "vector::push_back performance"
- Next in thread: Andrew Koenig: "Re: vector::push_back performance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|