std::list::clear()

S

scooter

std::list::clear() can slow down the performance of a method
considerably if it is called many times a second. I have countered
this problem a little by assigning a fixed size to a list when
instantiated and keeping track of the elements through iterator logic.
This seems a little messy to me though. I'm sure the guys who created
the STL must have a speedier method.

How do you guys clear and reuse a std::list multiple times with as
little slowdown as possible? (where the number of elements to be
inserted each iteration is unknown)

I preferably would like to use lists that have an undetermined size at
runtime.

thanks for any help.
 
A

Attila Feher

scooter said:
std::list::clear() can slow down the performance of a method
considerably if it is called many times a second. I have countered
this problem a little by assigning a fixed size to a list when
instantiated and keeping track of the elements through iterator logic.
This seems a little messy to me though. I'm sure the guys who created
the STL must have a speedier method.

How do you guys clear and reuse a std::list multiple times with as
little slowdown as possible? (where the number of elements to be
inserted each iteration is unknown)

I preferably would like to use lists that have an undetermined size at
runtime.

I don't use list if I need to clear it all the time. Then I use something
else, something less "slow to walk".

If I were you I would first test if it is really the list::clear, which is
expensive or your list elements (your classes) are expensive to destroy?

IMO you need something else not a list - or you need to place a reference to
your objects into the list, not a copy. It depends on what is really slow
in your program. A good profiler should tell.
 
T

tom_usenet

std::list::clear() can slow down the performance of a method
considerably if it is called many times a second. I have countered
this problem a little by assigning a fixed size to a list when
instantiated and keeping track of the elements through iterator logic.
This seems a little messy to me though. I'm sure the guys who created
the STL must have a speedier method.

How do you guys clear and reuse a std::list multiple times with as
little slowdown as possible? (where the number of elements to be
inserted each iteration is unknown)

I preferably would like to use lists that have an undetermined size at
runtime.

Using a fixed size object allocator for the list would likely remove
the bottleneck. e.g.

http://www.boost.org/libs/pool/doc/index.html

typedef std::list<Element, boost::pool_allocator<Element> >
ElementList_t;

Alternatively, pick a different container like vector or deque, which
have faster clear behaviour.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top