Boost logo

Boost Users :

Subject: Re: [Boost-users] Object Pool Usage
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-09-23 11:03:45


AMDG

david.weber_at_[hidden] wrote:
> So, I'm investigating Object Pools, and I think I'm not getting
> something. The documentation isn't great, hence the question
>
> We have a lot of small objects that we will use throughout our
> application's lifespan, and I'm trying to see if ObjectPool is a good
> fit for us. Heck, I'm just trying to look at performance of it in
> general.
>
> So, I have the following code:
>
>
> --------------------------------------------------------------
>
> void testNormal()
> {
> common::Timer t;
> t.start();
>
> for(int i = 0; i < MAX_NUM_TESTS; i++)
> {
> testClass t;
> t.doWork();
> }
> t.stop();
>
> std::cout << "No Pool: Creating " << MAX_NUM_TESTS << " took " <<
> str_cast(t.getTotalElapsedTime()) << std::endl;
> }
>
> void testPool()
> {
> common::Timer t;
> t.start();
> boost::object_pool<testClass> pool;
>
> for(int i = 0; i < MAX_NUM_TESTS; i++)
> {
> testClass::ptr tc = pool.construct();
> tc->doWork();
> }
>
> t.stop();
> std::cout << "Pool: Creating " << MAX_NUM_TESTS << " took " <<
> str_cast(t.getTotalElapsedTime()) << std::endl;
> }
>
> int main(int argc, char* argv[])
> {
> testNormal();
> testPool();
>
> return 0;
> }
> ------------------------------------------------
>
> When run, I am getting:
>
> No Pool: Creating 500000 took 00:00:00.187785
> Pool: Creating 500000 took 00:00:00.281679
>
>
>
> So, I guess I'm not understanding the advantage of the pools. Could
> someone write/modify my example to make it make more sense?
>

If you can use the stack as in testNormal, you shouldn't
use Boost.Pool.

If you want to test the speed of object_pool, you should compare
it to heap allocation.

void testNormal()
{
    common::Timer t;
    t.start();

    for(int i = 0; i < MAX_NUM_TESTS; i++)
    {
        std::auto_ptr<testClass> t(new testClass);
        t->doWork();
    }
    t.stop();

    std::cout << "No Pool: Creating " << MAX_NUM_TESTS
        << " took " << str_cast(t.getTotalElapsedTime())
        << std::endl;
}

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net