template< class T, typename Type > class test_type_and_smart_pointer { public: test_type_and_smart_pointer( std::string const& val, bool msg, Type type_val ) : _msg( msg ), _name( val ) { if( _msg ) std::cout << "created " << _name << std::endl; x.reset( new Type ); *x = type_val; } ~test_type_and_smart_pointer() { if( _msg ) std::cout << "deleted " << _name << std::endl; } test_type_and_smart_pointer(const test_type_and_smart_pointer & W) { if( W._msg ) std::cout << "copied " << W._name << std::endl; this->x.reset( new Type ); *(this->x) = *(Wx); this->_name = W._name; this->_msg = W._msg; } private: T x; bool _msg; std::string _name; };
template< class T, typename Type > class test_type_and_simple_pointer { public: test_type_and_simple_pointer( std::string const& val, bool msg, Type type_val ) : _msg( msg ), _name( val ) { if( _msg ) std::cout << "created " << _name << std::endl; x = new Type; *x = type_val; } ~test_type_and_simple_pointer() { delete x; if( _msg ) std::cout << "deleted " << _name << std::endl; } test_type_and_simple_pointer(const test_type_and_simple_pointer & W) { if( W._msg ) std::cout << "copied " << W._name << std::endl; this->x = new Type( *(Wx) ); this->_name = W._name; this->_msg = W._msg; } private: T x; bool _msg; std::string _name; };
template< typename _Ptr, typename T, typename Container, bool msg > void test_smart_ptr( bool big_data, T val ) { using namespace std; test_type_and_smart_pointer< _Ptr, T > x( "a001", msg, val ); test_type_and_smart_pointer< _Ptr, T > x2( x ); Container cnt; cnt.push_back( x ); cnt.push_back( x2 ); if( big_data ) for( int i = 0; i <= 100; i++ ) { test_type_and_smart_pointer< _Ptr, T > xx( "a002", msg, val ); test_type_and_smart_pointer< _Ptr, T > xx2( xx ); cnt.push_back( xx ); cnt.push_back( xx2 ); } cnt.clear(); } template< typename T, typename Container, bool msg > void test_simple_ptr( bool big_data, T val ) { using namespace std; test_type_and_simple_pointer< T*, T> x( "b001", msg, val ); test_type_and_simple_pointer< T*, T> x2( x ); Container cnt; cnt.push_back( x ); cnt.push_back( x2 ); if( big_data ) for( int i = 0; i <= 100; i++ ) { test_type_and_simple_pointer< T*, T> xx( "b002", msg, val ); test_type_and_simple_pointer< T*, T> xx2( xx ); cnt.push_back( xx ); cnt.push_back( xx2 ); } cnt.clear(); }
double _time_test( std::function< void() > test_func, unsigned int times ) { using namespace std; double start = 0, end = 0; start = GetTickCount(); for( unsigned int i = 0; i < times; i++ ) { test_func(); } end = GetTickCount() - start; //cout << "Elapsed ms: " << end << // " for " << times << " times " << endl; return end; }
#include "iostream" #include "string" #include "vector" #include "list" #include "deque " #include <Windows.h> #include "functional"
class some_obj { public: some_obj() { i = 90000; s = "1231231231231231231232"; d = 39482.27392; } ~some_obj() { } private: int i; std::string s; double d; };
int main() { using namespace std; const long long_val = 900000000; const char ch = 'a'; const string str = "abc"; /////////////////////////////////////////////////////////////////////////////////////////////////////// auto f_01 = [&]() -> void { test_smart_ptr< shared_ptr< long >, long, vector< test_type_and_smart_pointer< shared_ptr< long >, long > >, false >( false, long_val ); }; auto f_02 = [&]() -> void { test_smart_ptr< shared_ptr< char >, char, vector< test_type_and_smart_pointer< shared_ptr< char >, char > >, false >( false, ch ); }; auto f_03 = [&]() -> void { test_smart_ptr< shared_ptr< string >, string, vector< test_type_and_smart_pointer< shared_ptr< string >, string > >, false >( false, str ); }; auto f_04 = [&]() -> void { some_obj x; test_smart_ptr< shared_ptr< some_obj >, some_obj, vector< test_type_and_smart_pointer< shared_ptr< some_obj >, some_obj > >, false >( false, x ); }; ... _time_test( f_01, 100000 ); _time_test( f_02, 100000 ); _time_test( f_03, 100000 ); _time_test( f_04, 100000 ); ... return 0; }
Source: https://habr.com/ru/post/138550/
All Articles