Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. it's much slower when using boost::pool_allocator and boost::object_pool

it's much slower when using boost::pool_allocator and boost::object_pool

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsperformance
9 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    followait
    wrote on last edited by
    #1

    the test result on my computer:

    //case 1
    2153 // not using pool
    16 // using pool
    //case 2
    15 // not using pool
    32 // using pool
    // case 3
    2668 // not using pool
    14976 // using pool
    Press any key to continue . . .

    main.cpp

    #include #include "test.h"

    int wmain()
    {
    test0(10000);
    test1(10000);
    test2(10000);
    test3(10000);
    test4(10000);
    test5(10000);
    system("pause");
    return 0;
    }

    test.h

    #pragma once

    void test0(int loops);
    void test1(int loops);
    void test2(int loops);
    void test3(int loops);
    void test4(int loops);
    void test5(int loops);

    test.cpp

    #include
    #include
    #include
    #include
    #include
    #include
    #include "test.h"

    using namespace std;

    class AAA
    {
    public:
    AAA()
    {
    // cout << "con" << endl;
    }
    ~AAA()
    {
    // cout << "descon" << endl;
    }
    public:
    int a;
    char b;
    short int c;
    vector d;
    };

    void test0(int loops)
    {
    char ** p = new char*[loops];

    DWORD tick = GetTickCount();	
    
    for (int i=0; i memory\_pool(32);	
    
    for (int i=0; i v;
    for (int i=0; i > v;
    for (int i=0; i obj\_pool;	
    
    for (int i=0; i
    
    L S 2 Replies Last reply
    0
    • F followait

      the test result on my computer:

      //case 1
      2153 // not using pool
      16 // using pool
      //case 2
      15 // not using pool
      32 // using pool
      // case 3
      2668 // not using pool
      14976 // using pool
      Press any key to continue . . .

      main.cpp

      #include #include "test.h"

      int wmain()
      {
      test0(10000);
      test1(10000);
      test2(10000);
      test3(10000);
      test4(10000);
      test5(10000);
      system("pause");
      return 0;
      }

      test.h

      #pragma once

      void test0(int loops);
      void test1(int loops);
      void test2(int loops);
      void test3(int loops);
      void test4(int loops);
      void test5(int loops);

      test.cpp

      #include
      #include
      #include
      #include
      #include
      #include
      #include "test.h"

      using namespace std;

      class AAA
      {
      public:
      AAA()
      {
      // cout << "con" << endl;
      }
      ~AAA()
      {
      // cout << "descon" << endl;
      }
      public:
      int a;
      char b;
      short int c;
      vector d;
      };

      void test0(int loops)
      {
      char ** p = new char*[loops];

      DWORD tick = GetTickCount();	
      
      for (int i=0; i memory\_pool(32);	
      
      for (int i=0; i v;
      for (int i=0; i > v;
      for (int i=0; i obj\_pool;	
      
      for (int i=0; i
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Is there supposed to be a question here?

      I must get a clever new signature for 2011.

      CPalliniC F 2 Replies Last reply
      0
      • L Lost User

        Is there supposed to be a question here?

        I must get a clever new signature for 2011.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Richard MacCutchan wrote:

        Is there supposed to be a question here**?**

        I see a question mark there. :-D Pardon the silly humour, it's a rainy Sunday, here...

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        L 1 Reply Last reply
        0
        • L Lost User

          Is there supposed to be a question here?

          I must get a clever new signature for 2011.

          F Offline
          F Offline
          followait
          wrote on last edited by
          #4

          If using a memory pool, it should be faster, doesn't it?

          L 1 Reply Last reply
          0
          • CPalliniC CPallini

            Richard MacCutchan wrote:

            Is there supposed to be a question here**?**

            I see a question mark there. :-D Pardon the silly humour, it's a rainy Sunday, here...

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            CPallini wrote:

            Pardon the silly humour, it's a rainy Sunday, here.

            I didn't think you would notice the rain after yesterday's brilliance by your team.

            I must get a clever new signature for 2011.

            CPalliniC 1 Reply Last reply
            0
            • F followait

              If using a memory pool, it should be faster, doesn't it?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Possibly, but it depends on the implementation and how the objects/memory are managed.

              I must get a clever new signature for 2011.

              1 Reply Last reply
              0
              • L Lost User

                CPallini wrote:

                Pardon the silly humour, it's a rainy Sunday, here.

                I didn't think you would notice the rain after yesterday's brilliance by your team.

                I must get a clever new signature for 2011.

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Well, beating France is always a great satisfaction for all. Anyway, I know, you're more used to. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • F followait

                  the test result on my computer:

                  //case 1
                  2153 // not using pool
                  16 // using pool
                  //case 2
                  15 // not using pool
                  32 // using pool
                  // case 3
                  2668 // not using pool
                  14976 // using pool
                  Press any key to continue . . .

                  main.cpp

                  #include #include "test.h"

                  int wmain()
                  {
                  test0(10000);
                  test1(10000);
                  test2(10000);
                  test3(10000);
                  test4(10000);
                  test5(10000);
                  system("pause");
                  return 0;
                  }

                  test.h

                  #pragma once

                  void test0(int loops);
                  void test1(int loops);
                  void test2(int loops);
                  void test3(int loops);
                  void test4(int loops);
                  void test5(int loops);

                  test.cpp

                  #include
                  #include
                  #include
                  #include
                  #include
                  #include
                  #include "test.h"

                  using namespace std;

                  class AAA
                  {
                  public:
                  AAA()
                  {
                  // cout << "con" << endl;
                  }
                  ~AAA()
                  {
                  // cout << "descon" << endl;
                  }
                  public:
                  int a;
                  char b;
                  short int c;
                  vector d;
                  };

                  void test0(int loops)
                  {
                  char ** p = new char*[loops];

                  DWORD tick = GetTickCount();	
                  
                  for (int i=0; i memory\_pool(32);	
                  
                  for (int i=0; i v;
                  for (int i=0; i > v;
                  for (int i=0; i obj\_pool;	
                  
                  for (int i=0; i
                  
                  S Offline
                  S Offline
                  Stefan_Lang
                  wrote on last edited by
                  #8

                  Sorry for being late to reply, but since there doesn't seem to be a useful answer yet I thought I'd add some explanations. Looking at your code, the results you got from test0() and test(1) look ok, but those from test2() and test(3) are probably useless: std::vector has it's own method of maintaining a buffer for cases of repeated resizing, which is very similar to the one used in boost::pool. Thus it will not allocate memory once per loop, in fact there will probably only be a few dozen allocations forwarded to the boost::pool allocator. If you want to ensure one allocation per loop count, then I suggest you use std::list instead. I am not quite sure why object_pool is so slow, but I suspect the reason is the method free(). This method, to my knowledge, is disabled for pools of standard objects, because it is very inefficient (it is O(n) where n is the number of objects in the pool). The standard memory allocator (i. e. new/delete)), although quite slow, is still only O(1)! Therefore, the only sensible way to use boost::object_pool at this time is to not call the destroy() method at all! Don't worry, the moment the pool gets destroyed, all the objects will still be destructed properly, but that will be much more efficient than destroying each object individually. I've tried to use boost::pool 2 years ago for our application, but, in similar tests, found that it wasn't usable for our purposes. I eventually created my own implementation that dynamically frees (and destructs) objects at O(1), and considerably faster than new/delete (tested for up to 10 million allocations). If that's what you're looking for, I could make this into an article. It might take some time though.

                  F 1 Reply Last reply
                  0
                  • S Stefan_Lang

                    Sorry for being late to reply, but since there doesn't seem to be a useful answer yet I thought I'd add some explanations. Looking at your code, the results you got from test0() and test(1) look ok, but those from test2() and test(3) are probably useless: std::vector has it's own method of maintaining a buffer for cases of repeated resizing, which is very similar to the one used in boost::pool. Thus it will not allocate memory once per loop, in fact there will probably only be a few dozen allocations forwarded to the boost::pool allocator. If you want to ensure one allocation per loop count, then I suggest you use std::list instead. I am not quite sure why object_pool is so slow, but I suspect the reason is the method free(). This method, to my knowledge, is disabled for pools of standard objects, because it is very inefficient (it is O(n) where n is the number of objects in the pool). The standard memory allocator (i. e. new/delete)), although quite slow, is still only O(1)! Therefore, the only sensible way to use boost::object_pool at this time is to not call the destroy() method at all! Don't worry, the moment the pool gets destroyed, all the objects will still be destructed properly, but that will be much more efficient than destroying each object individually. I've tried to use boost::pool 2 years ago for our application, but, in similar tests, found that it wasn't usable for our purposes. I eventually created my own implementation that dynamically frees (and destructs) objects at O(1), and considerably faster than new/delete (tested for up to 10 million allocations). If that's what you're looking for, I could make this into an article. It might take some time though.

                    F Offline
                    F Offline
                    followait
                    wrote on last edited by
                    #9

                    Thanks, agree with you.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups