Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2000-02-01 05:48:57

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

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);
      typedef shared_ptr<int> ptr_int;
      printf("testing %d shared_ptr<int>\n", N);
   { 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);
      printf("sort list: %ld\n",(long)clock() - start);
   return 0;

Boost list run by bdawes at, gregod at, cpdaniel at, john at