Advertisement
Wyrd

shared_ptr tests

Feb 22nd, 2012
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.90 KB | None | 0 0
  1. #include <tchar.h>
  2. #include <iostream>
  3.  
  4. #include <Windows.h>
  5. #include <memory>
  6.  
  7. #define BOOST_HAS_RVALUE_REFS
  8.  
  9. #include <d:\Coding\MSVS 2010\VC\boost_1_48_0\boost\config\compiler\visualc.hpp>
  10. #include <d:\Coding\MSVS 2010\VC\boost_1_48_0\boost\shared_ptr.hpp>
  11. #include <d:\Coding\MSVS 2010\VC\boost_1_48_0\boost\make_shared.hpp>
  12.  
  13. #include "e:\Coding\URS\Repository\FinancialEventDescriptionRefactoring\COMMON\stl\fast_pointer_cast.hpp"
  14. #include "e:\Coding\URS\Repository\FinancialEventDescriptionRefactoring\COMMON\SmallObjectAllocator.hpp"
  15.  
  16. class CTimer
  17. {
  18.     static double TicksPerSecond()
  19.     {
  20.         LARGE_INTEGER nFreq;
  21.  
  22.         ::QueryPerformanceFrequency( &nFreq );
  23.  
  24.         return nFreq.QuadPart / 1000000.0;
  25.     }
  26.  
  27. public:
  28.  
  29.     CTimer()
  30.     {
  31.         reset();
  32.     }
  33.  
  34.     //! Returns current (system) time in microseconds.
  35.     double tick() const
  36.     {
  37.         LARGE_INTEGER nCounter;
  38.  
  39.         ::QueryPerformanceCounter( &nCounter );
  40.  
  41.         return nCounter.QuadPart / m_sdTicksPerSecond;
  42.     }
  43.  
  44.     //! Resets timer and returns current (system) time in microseconds.
  45.     double reset()
  46.     {
  47.         m_dLast = tick();
  48.         return m_dLast;
  49.     }
  50.  
  51.     //! Returns elapsed time in microseconds since last reset(), then resets timer.
  52.     double lap()
  53.     {
  54.         double t = m_dLast;
  55.         m_dLast  = tick();
  56.         return m_dLast - t;
  57.     }
  58.  
  59. private:
  60.  
  61.     double        m_dLast;
  62.     static double m_sdTicksPerSecond;
  63. };
  64.  
  65. double CTimer::m_sdTicksPerSecond = CTimer::TicksPerSecond();
  66.  
  67. #define N 100000000
  68.  
  69. template< class T >
  70. void DoTest( const T &rcf )
  71. {
  72.     CTimer oTimer;
  73.  
  74.     rcf();
  75.  
  76.     double d = oTimer.lap() * 1e3 / double( N );
  77.  
  78.     std::cout << "const of one iteration: " << d << " ns." << std::endl;
  79. };
  80.  
  81. class CBase
  82. {
  83. public:
  84.  
  85.     virtual ~CBase()
  86.     {
  87.     }
  88. };
  89.  
  90. class CDerived
  91.     :   public CBase
  92. {
  93. };
  94.  
  95. int _tmain( int argc, _TCHAR* argv[] )
  96. {
  97.     SetThreadAffinityMask( GetCurrentThread(), 1 );
  98.  
  99.  
  100.     std::cout << "std, make_shared:                 "; DoTest(
  101.         []()
  102.         {
  103.             for( size_t i = 0; i < N; ++i ) std::make_shared< CDerived >();
  104.         } );
  105.  
  106.     std::cout << "boost, make_shared:               "; DoTest(
  107.         []()
  108.         {
  109.             for( size_t i = 0; i < N; ++i ) boost::make_shared< CDerived >();
  110.         } );
  111.  
  112.     std::cout << std::endl;
  113.  
  114.  
  115.     std::cout << "std, make_shared + cast:          "; DoTest(
  116.         []()
  117.         {
  118.             for( size_t i = 0; i < N; ++i ) std::static_pointer_cast< CBase >( std::make_shared< CDerived >() );
  119.         } );
  120.    
  121.     std::cout << "std, make_shared + fast cast:     "; DoTest(
  122.         []()
  123.         {
  124.             for( size_t i = 0; i < N; ++i ) stl::fast_static_pointer_cast< CBase >( std::make_shared< CDerived >() );
  125.         } );
  126.  
  127.     std::cout << "boost, make_shared + cast:        "; DoTest(
  128.         []()
  129.         {
  130.             for( size_t i = 0; i < N; ++i ) boost::static_pointer_cast< CBase >( boost::make_shared< CDerived >() );
  131.         } );
  132.  
  133.     std::cout << std::endl;
  134.  
  135.  
  136.     std::cout << "std, allocate_shared:             "; DoTest(
  137.         []()
  138.         {
  139.             for( size_t i = 0; i < N; ++i ) std::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() );
  140.         } );
  141.  
  142.     std::cout << "boost, allocate_shared:           "; DoTest(
  143.         []()
  144.         {
  145.             for( size_t i = 0; i < N; ++i ) boost::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() );
  146.         } );
  147.  
  148.     std::cout << std::endl;
  149.    
  150.  
  151.     std::cout << "std, allocate_shared + cast:      "; DoTest(
  152.         []()
  153.         {
  154.             for( size_t i = 0; i < N; ++i ) std::static_pointer_cast< CBase >( std::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() ) );
  155.         } );
  156.  
  157.     std::cout << "std, allocate_shared + fast cast: "; DoTest(
  158.         []()
  159.         {
  160.             for( size_t i = 0; i < N; ++i ) stl::fast_static_pointer_cast< CBase >( std::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() ) );
  161.         } );
  162.  
  163.     std::cout << "boost, allocate_shared + cast:    "; DoTest(
  164.         []()
  165.         {
  166.             for( size_t i = 0; i < N; ++i ) boost::static_pointer_cast< CBase >( boost::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() ) );
  167.         } );
  168.  
  169.     std::cout << std::endl;
  170.  
  171.  
  172.     std::cout << "std, release:                     "; DoTest(
  173.         []()
  174.         {
  175.             for( size_t i = 0; i < N; ++i ){ std::shared_ptr< CDerived > p( new CDerived() ); }
  176.         } );
  177.  
  178.     std::cout << "boost, release:                   "; DoTest(
  179.         []()
  180.         {
  181.             for( size_t i = 0; i < N; ++i ){ boost::shared_ptr< CDerived > p( new CDerived() ); }
  182.         } );
  183.  
  184.     std::cout << std::endl;
  185.  
  186.  
  187.     std::cout << "std, release + cast:              "; DoTest(
  188.         []()
  189.         {
  190.             for( size_t i = 0; i < N; ++i ){ std::shared_ptr< CDerived > p( new CDerived() ); std::static_pointer_cast< CBase >( std::move( p ) ); }
  191.         } );
  192.  
  193.     std::cout << "std, release + fast cast:         "; DoTest(
  194.         []()
  195.         {
  196.             for( size_t i = 0; i < N; ++i ){ std::shared_ptr< CDerived > p( new CDerived() ); stl::fast_static_pointer_cast< CBase >( std::move( p ) ); }
  197.         } );
  198.  
  199.     std::cout << "boost, release + cast:            "; DoTest(
  200.         []()
  201.         {
  202.             for( size_t i = 0; i < N; ++i ){ boost::shared_ptr< CDerived > p( new CDerived() ); boost::static_pointer_cast< CBase >( std::move( p ) ); }
  203.         } );
  204.  
  205.     return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement