boost fast_pool_allocator performance question

Discussion in 'C++' started by absurd, Mar 30, 2010.

  1. absurd

    absurd Guest

    Hello,

    I have a very simple program trying to see how well the boost fast
    pool allocator is, but I don't get good results.

    My program looks like,

    main.cpp

    #include <iostream>
    #include <list>
    #include <iterator>
    #include <boost/pool/pool_alloc.hpp>

    #include "Clock.h"

    int CNT = 0;
    unsigned int NUM = 1000000;

    template <typename L>
    void test_list( L & l )
    {
    Clock clock( CNT++);
    for ( int i = 0; i < 3; i++ )
    {
    for (int j = 0; j < NUM; ++j)
    {
    typename L::value_type obj(j);
    l.push_back( obj );
    }


    template <unsigned int SIZE>
    struct Bytes
    {
    char bytes[SIZE];
    Bytes( unsigned int i ) {}
    };


    int main()
    {
    std::list< Bytes<4> > l;
    test_list( l );

    std::list<int, boost::fast_pool_allocator<Bytes<4> > > pl;
    test_list( pl );
    }


    Clock.h


    #ifndef CLOCK_H
    #define CLOCK_H
    #include <iostream>

    class Clock
    {
    public:
    static long long diff( const timespec & start, const timespec &
    end )
    {
    return (( end.tv_sec - start.tv_sec ) * 1000000000 +
    ( end.tv_nsec - start.tv_nsec )) / 1000;
    }

    Clock( int id )
    : m_id( id )
    {
    clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &m_start );
    //clock_gettime( CLOCK_THREAD_CPUTIME_ID, &m_start );
    }

    ~Clock()
    {
    clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &m_end );
    std::cout << "t," << m_id << "," << diff( m_start, m_end ) <<
    std::endl;
    }

    private:
    int m_id;
    timespec m_start;
    timespec m_end;

    };

    #endif


    results,
    $ ./a.out
    t,0,181820
    t,1,266289

    As you see from the above results, boost version is significantly
    slower. Am I using the boost allocator correctly ?

    Thanks.
    absurd, Mar 30, 2010
    #1
    1. Advertising

  2. absurd

    absurd Guest

    Correction:

    The following line :
    std::list<int, boost::fast_pool_allocator<Bytes<4> > > pl;

    should be
    std::list< Bytes<4>, boost::fast_pool_allocator<Bytes<4> > > pl;


    On Mar 30, 3:33 pm, absurd <> wrote:
    > Hello,
    >
    > I have a very simple program trying to see how well the boost fast
    > pool allocator is, but I don't get good results.
    >
    > My program looks like,
    >
    > main.cpp
    >
    > #include <iostream>
    > #include <list>
    > #include <iterator>
    > #include <boost/pool/pool_alloc.hpp>
    >
    > #include "Clock.h"
    >
    > int CNT = 0;
    > unsigned int NUM = 1000000;
    >
    > template <typename L>
    > void test_list( L & l )
    > {
    >     Clock clock( CNT++);
    >     for ( int i = 0; i < 3; i++ )
    >     {
    >         for (int j = 0; j < NUM; ++j)
    >         {
    >             typename L::value_type obj(j);
    >             l.push_back( obj );
    >         }
    >
    > template <unsigned int SIZE>
    > struct Bytes
    > {
    >     char bytes[SIZE];
    >     Bytes( unsigned int i ) {}
    >
    > };
    >
    > int main()
    > {
    >     std::list< Bytes<4> > l;
    >     test_list( l );
    >
    >     std::list<int, boost::fast_pool_allocator<Bytes<4> > > pl;
    >     test_list( pl );
    >
    > }
    >
    > Clock.h
    >
    > #ifndef CLOCK_H
    > #define CLOCK_H
    > #include <iostream>
    >
    > class Clock
    > {
    > public:
    >     static long long diff( const timespec & start, const timespec &
    > end )
    >     {
    >         return (( end.tv_sec - start.tv_sec ) * 1000000000 +
    >                ( end.tv_nsec - start.tv_nsec )) / 1000;
    >     }
    >
    >     Clock( int id )
    >         : m_id( id )
    >     {
    >         clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &m_start );
    >         //clock_gettime( CLOCK_THREAD_CPUTIME_ID, &m_start );
    >     }
    >
    >     ~Clock()
    >     {
    >         clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &m_end );
    >         std::cout << "t," << m_id << "," << diff( m_start, m_end ) <<
    > std::endl;
    >     }
    >
    > private:
    >     int m_id;
    >     timespec m_start;
    >     timespec m_end;
    >
    > };
    >
    > #endif
    >
    > results,
    > $ ./a.out
    > t,0,181820
    > t,1,266289
    >
    > As you see from the above results, boost version is significantly
    > slower.  Am I using the boost allocator correctly ?
    >
    > Thanks.
    absurd, Mar 30, 2010
    #2
    1. Advertising

  3. On Mar 31, 4:33 am, absurd <> wrote:
    > Hello,
    >
    > I have a very simple program trying to see how well the boost fast
    > pool allocator is, but I don't get good results.
    >
    > My program looks like,
    >
    > main.cpp
    >
    > #include <iostream>
    > #include <list>
    > #include <iterator>
    > #include <boost/pool/pool_alloc.hpp>
    >
    > #include "Clock.h"
    >
    > int CNT = 0;
    > unsigned int NUM = 1000000;
    >
    > template <typename L>
    > void test_list( L & l )
    > {
    >     Clock clock( CNT++);
    >     for ( int i = 0; i < 3; i++ )
    >     {
    >         for (int j = 0; j < NUM; ++j)
    >         {
    >             typename L::value_type obj(j);
    >             l.push_back( obj );
    >         }
    >


    Your test is just allocating objects into the list. That is not the
    case for which the pool allocators are suggested for. They are
    designed for use when you have frequent allocations as well as
    deallocations. So, to simulate that you would need also to remove
    elements from the list and re-insert and so on. The current slowness
    could be because of the underlying pool that the allocator needs to
    maintain.
    Abhishek Padmanabh, Apr 2, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Latter
    Replies:
    2
    Views:
    2,882
    Julie
    May 17, 2004
  2. Steve Knight
    Replies:
    2
    Views:
    739
    Steve Knight
    Oct 10, 2003
  3. Toby Bradshaw
    Replies:
    6
    Views:
    1,712
    Kai-Uwe Bux
    Jun 2, 2006
  4. Colin Caughie
    Replies:
    1
    Views:
    690
    Shooting
    Aug 29, 2006
  5. Misiu
    Replies:
    3
    Views:
    2,340
    Misiu
    Jan 31, 2007
Loading...

Share This Page