Click here to Skip to main content
11,643,493 members (74,781 online)
Click here to Skip to main content
Add your own
alternative version

The fastest smart pointer in the west

, 7 Oct 2003 93.2K 1.5K 40
Need a super quick implementation of a reference counting smart pointer? AUTO_REF delivers just that, with a 'thin' source code of just 3,886 bytes (< 4KB)
// e_ver.cpp : Defines the entry point for the console application.
//
// Objective: simulate an algorithm which uses pointers for benchmarking puposes
//


#pragma warning(disable:4786) 



#include "setup.h"





//=====================================================================
//=====================================================================
//=====================================================================

#include <cassert>
#include <windows.h>
#include <vector>
#include <iostream>

#include "auto_ref.h"
#include "other/SmartPtr_a.h"
#include "other/SmartPtr_b.h"
// #include "other/SmartPtr_c.h"
#include <boost/smart_ptr.hpp>



using std::cout;
using std::endl;
using std::dec;












//=====================================================================
//=====================================================================
//=====================================================================









class MY_CLASS
{
public:
   

#if PTR_SWITCH == 40
   typedef AUTO_REF<MY_CLASS> MY_PTR;  //A
#elif PTR_SWITCH == 50
   typedef MY_CLASS *MY_PTR;  //B
#elif PTR_SWITCH == 60
   typedef RefCountPtr<MY_CLASS> MY_PTR; //C
#elif PTR_SWITCH == 70
   typedef idllib::SmartPtr<MY_CLASS> MY_PTR;  //D
#elif PTR_SWITCH == 80
   typedef boost::shared_ptr<MY_CLASS> MY_PTR;  //E
#elif PTR_SWITCH == 90
   typedef GE::Safe::PtrStrong<MY_CLASS> MY_PTR;  //E
#else
#  error illegal PTR_SWITCH value

#endif //PTR_SWITCH



private:
   int m_int_val;
   bool is_set_b;
   MY_PTR m_friend_p;


public:
   MY_CLASS(int int_val = 0) : m_int_val(int_val), is_set_b(false) {}
   ~MY_CLASS() {
#ifdef VERBOSE
      cout << "shutdown of [ " << dec << m_int_val << "]" << endl;
#endif
   }

   void show()  {
      if (is_set_b)
         cout << " me: " << m_int_val << " -> " << m_friend_p->m_int_val
            << "(frnd) "<< endl;
      else
         cout << " me: " << m_int_val << " -> " << "(no frnd) " 
            << endl;
   }

   int get_val() {
      return m_int_val;
   }

   void set_friend(const MY_PTR &x) {
      is_set_b = true;
      m_friend_p = x;
   }

   int get_friend_val()  {
      if(is_set_b)
         return m_friend_p->get_val();
      
      return -1;
   }
};





//=====================================================================
//=====================================================================
//=====================================================================



typedef MY_CLASS::MY_PTR MY_PTR;
typedef std::vector<MY_PTR> my_vec_t;



#ifdef VERBOSE
enum { 
   LIMIT = 20 
};
#else
enum { 
   LIMIT = 320000 
};
#endif



//=====================================================================
//=====================================================================
//=====================================================================


inline MY_PTR &get_at(my_vec_t *vec_p, int index) {
   return (*vec_p)[index];
}

void print_all(my_vec_t &vec) {
   for (my_vec_t::size_type i = 0 ; i < vec.size(); ++i)
      get_at(&vec, i)->show();
}



#if PTR_SWITCH == 50
void delete_objects(my_vec_t &vec) {
   my_vec_t::iterator last(vec.end());
   for (my_vec_t::iterator i = vec.begin() ; i != last ; ++i) 
      delete (*i);
}
#endif

void fill(my_vec_t **vec_pp, int limit) {

   *vec_pp = new my_vec_t;

   for (int i = 0 ; i < limit ; ++i)
      (*vec_pp)->push_back( MY_PTR(new MY_CLASS(10000+i)) );
}



//=====================================================================
//=====================================================================
//=====================================================================



void main_p()
{


   my_vec_t *vec_p;

   fill(&vec_p, LIMIT);



#ifdef VERBOSE
   print_all(*vec_p);
#endif
   for (int i = 0 ; i < LIMIT * 2 ; i++)
   {
      int x = rand() % ((LIMIT-2)/2);

      int y = LIMIT/2 + rand() % ((LIMIT-2)/2);

#ifdef VERBOSE
      cout << x <<  " -> " << y << endl;
#endif

      get_at(vec_p, x)->set_friend( get_at(vec_p, y));

   }

   int s = 0;
   for (i = 0 ; i < LIMIT * 60 ; i++) {
      int x = rand() % LIMIT;
      int l = get_at(vec_p, x)->get_friend_val();
      s += l;
   }

#ifdef VERBOSE
   print_all(*vec_p);
#endif

#if PTR_SWITCH == 50
   delete_objects(*vec_p);
#endif
   delete vec_p;

} //main_p()






//=====================================================================
//=====================================================================
//=====================================================================


void check_mem()
{
   std::vector<void*> ptrs;
   
   int limit = 20;
   for(int i = 0; i < limit; ++i)
      ptrs.push_back(g_the_auto_ref_pool.malloc());

   int sz = ptrs.size();      
   while(sz > 0)
   {
      int pos = rand() % sz;
      g_the_auto_ref_pool.free(ptrs[pos]);
      std::swap(ptrs[pos], ptrs[sz - 1]);
      
      sz -= 1;
   }      
   
   for(i = 0; i < limit; ++i)
   {
      void* temp = g_the_auto_ref_pool.malloc();
      assert(temp == ptrs[i]);
   } 
}

int main(int argc, char* argv[]) 
{   
   check_mem();
   
   DWORD t0 = GetTickCount();

   main_p();


   DWORD t1 = GetTickCount();



#if 1
   cout << "run time = " << (t1-t0)/1000.0 << endl;
#endif

   cout << "main is done!" << endl;
	return 0;
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Itay Maman
Israel Israel
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 8 Oct 2003
Article Copyright 2001 by Itay Maman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid