std::list::clear()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scooter

    std::list::clear()

    std::list::clea r() 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.
  • Attila Feher

    #2
    Re: std::list::clea r()

    scooter wrote:[color=blue]
    > std::list::clea r() 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.[/color]

    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.

    --
    Attila aka WW


    Comment

    • jean Davy

      #3
      Re: std::list::clea r()

      use "resize( 0 )"
      but optimizing is a full task

      "scooter" <scooter@btopen world.com> a écrit dans le message de
      news:r87onvgdtr aq52oemuvqu8t3r ta24b8t61@4ax.c om...[color=blue]
      > std::list::clea r() 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.[/color]


      Comment

      • tom_usenet

        #4
        Re: std::list::clea r()

        On Thu, 02 Oct 2003 18:19:37 +0530, scooter <scooter@btopen world.com>
        wrote:
        [color=blue]
        >std::list::cle ar() 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.[/color]

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



        typedef std::list<Eleme nt, boost::pool_all ocator<Element> >
        ElementList_t;

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

        Tom

        Comment

        Working...