Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tchar.h>
- #include <iostream>
- #include <Windows.h>
- #include <memory>
- #define BOOST_HAS_RVALUE_REFS
- #include <d:\Coding\MSVS 2010\VC\boost_1_48_0\boost\config\compiler\visualc.hpp>
- #include <d:\Coding\MSVS 2010\VC\boost_1_48_0\boost\shared_ptr.hpp>
- #include <d:\Coding\MSVS 2010\VC\boost_1_48_0\boost\make_shared.hpp>
- #include "e:\Coding\URS\Repository\FinancialEventDescriptionRefactoring\COMMON\stl\fast_pointer_cast.hpp"
- #include "e:\Coding\URS\Repository\FinancialEventDescriptionRefactoring\COMMON\SmallObjectAllocator.hpp"
- class CTimer
- {
- static double TicksPerSecond()
- {
- LARGE_INTEGER nFreq;
- ::QueryPerformanceFrequency( &nFreq );
- return nFreq.QuadPart / 1000000.0;
- }
- public:
- CTimer()
- {
- reset();
- }
- //! Returns current (system) time in microseconds.
- double tick() const
- {
- LARGE_INTEGER nCounter;
- ::QueryPerformanceCounter( &nCounter );
- return nCounter.QuadPart / m_sdTicksPerSecond;
- }
- //! Resets timer and returns current (system) time in microseconds.
- double reset()
- {
- m_dLast = tick();
- return m_dLast;
- }
- //! Returns elapsed time in microseconds since last reset(), then resets timer.
- double lap()
- {
- double t = m_dLast;
- m_dLast = tick();
- return m_dLast - t;
- }
- private:
- double m_dLast;
- static double m_sdTicksPerSecond;
- };
- double CTimer::m_sdTicksPerSecond = CTimer::TicksPerSecond();
- #define N 100000000
- template< class T >
- void DoTest( const T &rcf )
- {
- CTimer oTimer;
- rcf();
- double d = oTimer.lap() * 1e3 / double( N );
- std::cout << "const of one iteration: " << d << " ns." << std::endl;
- };
- class CBase
- {
- public:
- virtual ~CBase()
- {
- }
- };
- class CDerived
- : public CBase
- {
- };
- int _tmain( int argc, _TCHAR* argv[] )
- {
- SetThreadAffinityMask( GetCurrentThread(), 1 );
- std::cout << "std, make_shared: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) std::make_shared< CDerived >();
- } );
- std::cout << "boost, make_shared: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) boost::make_shared< CDerived >();
- } );
- std::cout << std::endl;
- std::cout << "std, make_shared + cast: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) std::static_pointer_cast< CBase >( std::make_shared< CDerived >() );
- } );
- std::cout << "std, make_shared + fast cast: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) stl::fast_static_pointer_cast< CBase >( std::make_shared< CDerived >() );
- } );
- std::cout << "boost, make_shared + cast: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) boost::static_pointer_cast< CBase >( boost::make_shared< CDerived >() );
- } );
- std::cout << std::endl;
- std::cout << "std, allocate_shared: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) std::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() );
- } );
- std::cout << "boost, allocate_shared: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) boost::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() );
- } );
- std::cout << std::endl;
- std::cout << "std, allocate_shared + cast: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) std::static_pointer_cast< CBase >( std::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() ) );
- } );
- std::cout << "std, allocate_shared + fast cast: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) stl::fast_static_pointer_cast< CBase >( std::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() ) );
- } );
- std::cout << "boost, allocate_shared + cast: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ) boost::static_pointer_cast< CBase >( boost::allocate_shared< CDerived >( TDefaultSmallObjectAllocator() ) );
- } );
- std::cout << std::endl;
- std::cout << "std, release: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ){ std::shared_ptr< CDerived > p( new CDerived() ); }
- } );
- std::cout << "boost, release: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ){ boost::shared_ptr< CDerived > p( new CDerived() ); }
- } );
- std::cout << std::endl;
- std::cout << "std, release + cast: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ){ std::shared_ptr< CDerived > p( new CDerived() ); std::static_pointer_cast< CBase >( std::move( p ) ); }
- } );
- std::cout << "std, release + fast cast: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ){ std::shared_ptr< CDerived > p( new CDerived() ); stl::fast_static_pointer_cast< CBase >( std::move( p ) ); }
- } );
- std::cout << "boost, release + cast: "; DoTest(
- []()
- {
- for( size_t i = 0; i < N; ++i ){ boost::shared_ptr< CDerived > p( new CDerived() ); boost::static_pointer_cast< CBase >( std::move( p ) ); }
- } );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement