Loading ...
Sorry, an error occurred while loading the content.

[boost] Re: faster shared_ptr?

Expand Messages
  • Greg Colvin
    I have appended a simple test that fills up and sorts a list and a vector of allocated ints, using either a raw or a smart pointer. Here are some results (best
    Message 1 of 10 , Feb 1, 2000
    • 0 Attachment
      I have appended a simple test that fills up and sorts a list
      and a vector of allocated ints, using either a raw or a smart
      pointer.

      Here are some results (best of 3 runs) for the appended test:

      testing 1000000 int*
      fill vector: 1015
      sort vector: 1437
      fill list: 1672
      sort list: 8547

      testing 1000000 shared_ptr<int>
      fill vector: 2968
      sort vector: 4781
      fill list: 2516
      sort list: 10485

      I like this test because it is the paradigm use for shared_ptr,
      which was a smart pointer that can be used with the standard
      containers and algorithms.

      This test shows that the convenience of smart pointer is far
      from free, and that pushing for better performance is not a
      waste of time. I would love to see the results of this test
      for various implementation strategies on various systems.

      Note that to compile this test with my older MSVC compiler I
      had to hack an operator<() member into shared_ptr. YMMV.


      #include <vector>
      #include <list>
      #include <algorithm>
      #include <stddef.h>
      #include <stdlib.h>
      #include <stdio.h>
      #include <time.h>
      #include <boost/smart_ptr.h>

      using namespace std;
      using namespace boost;

      int main(int argc, char** argv) {
      const int N = atoi(argv[1]);
      #ifdef RAW
      typedef int* ptr_int;
      printf("testing %d int* \n", N);
      #else
      typedef shared_ptr<int> ptr_int;
      printf("testing %d shared_ptr<int>\n", N);
      #endif
      { clock_t start = clock();
      vector<ptr_int> container;
      for (int i = 0; i < N; i++ )
      container.push_back(ptr_int(new int(rand())));
      printf("fill vector: %ld\n",(long)clock() - start);
      sort(container.begin(), container.end());
      printf("sort vector: %ld\n",(long)clock() - start);
      }
      { clock_t start = clock();
      list<ptr_int> container;
      for (int i = 0; i < N; i++ )
      container.push_back(ptr_int(new int(rand())));
      printf("fill list: %ld\n",(long)clock() - start);
      container.sort();
      printf("sort list: %ld\n",(long)clock() - start);
      }
      return 0;
      }
    • Miki Jovanovic
      ... Hi Greg, I just wanted to point out that using smart pointers is not a convinience, it is much closer to necessity. If you even mention the word exception
      Message 2 of 10 , Feb 1, 2000
      • 0 Attachment
        "greg colvin" <gcolvi-@...> wrote:
        > I like this test because it is the paradigm use for shared_ptr,
        > which was a smart pointer that can be used with the standard
        > containers and algorithms.
        >
        > This test shows that the convenience of smart pointer is far
        > from free, and that pushing for better performance is not a
        > waste of time. I would love to see the results of this test
        > for various implementation strategies on various systems.

        Hi Greg,

        I just wanted to point out that using smart pointers is not a
        convinience, it is much closer to necessity. If you even mention the
        word exception in your application, you will almost invariably end up
        using a kind of smart pointer (maybe only auto_ptr in simple cases). So
        comparing a useful tool with a simple pointer is not a valid comparison.

        I do agree with you on one account, performance should be looked at
        carefully.

        Cheers,

        Miki Jovanovic.
      • Greg Colvin
        For those who think that the challenge of C++ memory management means we should just use Java (this is with Sun s latest JIT) ... D: greg Boost java TestPtr
        Message 3 of 10 , Feb 2, 2000
        • 0 Attachment
          For those who think that the challenge of C++ memory management
          means we should just use Java (this is with Sun's latest JIT) ...

          D:\greg\Boost>java TestPtr 1000000
          fill array: 5281
          sort array: 18672

          D:\greg\Boost>test_ptr 1000000
          testing 1000000 shared_ptr<int>
          fill vector: 1859
          sort vector: 1812

          D:\greg\Boost>test_ptr 1000000
          testing 1000000 int*
          fill vector: 843
          sort vector: 422


          ////////////////////////////////////////////////////////////////////////////////
          import java.util.*;
          class TestPtr {
          public static void main(String args[]) {
          final int N = Integer.parseInt(args[0]);
          Random random = new Random();
          long start = System.currentTimeMillis();
          Integer[] array = new Integer[N];
          for (int i = 0; i < N; i++ )
          array[i] = new Integer(random.nextInt());
          System.out.println("fill array: " + (System.currentTimeMillis() - start));
          start = System.currentTimeMillis();
          Arrays.sort(array);
          System.out.println("sort array: " + (System.currentTimeMillis() - start));

          }
          }


          ////////////////////////////////////////////////////////////////////////////////
          #include <vector>
          #include <algorithm>
          #include <stddef.h>
          #include <stdlib.h>
          #include <stdio.h>
          #include <time.h>
          #include <boost/smart_ptr.hpp>
          using namespace std;
          using namespace boost;
          int main(int argc, char** argv) {
          const int N = atoi(argv[1]);
          #ifdef RAW
          typedef int* ptr_int;
          printf("testing %d int* \n", N);
          #else
          typedef shared_ptr<int> ptr_int;
          printf("testing %d shared_ptr<int>\n", N);
          #endif
          { clock_t start = clock();
          vector<ptr_int> container(N);;
          for (int i = 0; i < N; i++ )
          container[i] = ptr_int(new int(rand()));
          printf("fill vector: %ld\n",
          ((long)clock() - start)*1000/CLOCKS_PER_SEC);
          start = clock();
          sort(container.begin(), container.end());
          printf("sort vector: %ld\n",
          ((long)clock() - start)*1000/CLOCKS_PER_SEC);
          }
          return 0;
          }
        • Miki Jovanovic
          ... vs. ... MSDN says: clock() returns the number of clock ticks of elapsed processor time. vs. currentTimeMillis() returns the difference, measured in
          Message 4 of 10 , Feb 2, 2000
          • 0 Attachment
            "greg colvin" <gcolvi-@...> wrote:
            > long start = System.currentTimeMillis();

            vs.

            > clock_t start = clock();

            MSDN says:

            "clock() returns the number of clock ticks of elapsed processor time."

            vs.

            currentTimeMillis() returns the difference, measured in milliseconds,
            between the current time and midnight, January 1, 1970 UTC.

            Would this processor time vs real time make much of a difference here?

            Cheers,

            Miki Jovanovic.
          • Greg Colvin
            From: Miki Jovanovic ... No, I print the elapsed time in milliseconds: printf( fill vector: %ld n , ((long)clock() -
            Message 5 of 10 , Feb 2, 2000
            • 0 Attachment
              From: Miki Jovanovic <miki@...>
              > "greg colvin" <gcolvi-@...> wrote:
              > > long start = System.currentTimeMillis();
              >
              > vs.
              >
              > > clock_t start = clock();
              >
              > MSDN says:
              >
              > "clock() returns the number of clock ticks of elapsed processor time."
              >
              > vs.
              >
              > currentTimeMillis() returns the difference, measured in milliseconds,
              > between the current time and midnight, January 1, 1970 UTC.
              >
              > Would this processor time vs real time make much of a difference here?

              No, I print the elapsed time in milliseconds:

              printf("fill vector: %ld\n",
              ((long)clock() - start)*1000/CLOCKS_PER_SEC
            • Greg Colvin
              From: Greg Colvin ... Never mind -- I see what you mean. I doubt it matters on my system, but I ll recode my C++ test to use real
              Message 6 of 10 , Feb 2, 2000
              • 0 Attachment
                From: Greg Colvin <gcolvin@...>
                > From: Miki Jovanovic <miki@...>
                > > "greg colvin" <gcolvi-@...> wrote:
                > > > long start = System.currentTimeMillis();
                > >
                > > vs.
                > >
                > > > clock_t start = clock();
                > >
                > > MSDN says:
                > >
                > > "clock() returns the number of clock ticks of elapsed processor time."
                > >
                > > vs.
                > >
                > > currentTimeMillis() returns the difference, measured in milliseconds,
                > > between the current time and midnight, January 1, 1970 UTC.
                > >
                > > Would this processor time vs real time make much of a difference here?
                >
                > No, I print the elapsed time in milliseconds:
                >
                > printf("fill vector: %ld\n",
                > ((long)clock() - start)*1000/CLOCKS_PER_SEC

                Never mind -- I see what you mean. I doubt it matters on my
                system, but I'll recode my C++ test to use real time. Thanks.
              • Greg Colvin
                From: Greg Colvin ... Done. Identical results.
                Message 7 of 10 , Feb 2, 2000
                • 0 Attachment
                  From: Greg Colvin <gcolvin@...>
                  > From: Greg Colvin <gcolvin@...>
                  > > From: Miki Jovanovic <miki@...>
                  > > > "greg colvin" <gcolvi-@...> wrote:
                  > > > > long start = System.currentTimeMillis();
                  > > >
                  > > > vs.
                  > > >
                  > > > > clock_t start = clock();
                  > > >
                  > > > MSDN says:
                  > > >
                  > > > "clock() returns the number of clock ticks of elapsed processor time."
                  > > >
                  > > > vs.
                  > > >
                  > > > currentTimeMillis() returns the difference, measured in milliseconds,
                  > > > between the current time and midnight, January 1, 1970 UTC.
                  > > >
                  > > > Would this processor time vs real time make much of a difference here?
                  > >
                  > > No, I print the elapsed time in milliseconds:
                  > >
                  > > printf("fill vector: %ld\n",
                  > > ((long)clock() - start)*1000/CLOCKS_PER_SEC
                  >
                  > Never mind -- I see what you mean. I doubt it matters on my
                  > system, but I'll recode my C++ test to use real time. Thanks.

                  Done. Identical results.
                Your message has been successfully submitted and would be delivered to recipients shortly.