Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- #include <vector>
- #include <boost/container/vector.hpp>
- #include <windows.h>
- #define COMPONENTS_COUNT 1000000
- // Just a basic movable object for my tests
- class Component
- {
- public :
- Component( void ) :
- id( 0 ),
- name( "default" ),
- data( 100 )
- {
- }
- Component( uint32_t id ) :
- id( id ),
- name( "default" ),
- data( 100 )
- {
- }
- Component( Component&& component ) throw( ) :
- id( std::move( component.id ) ),
- name( std::move( component.name ) ),
- data( std::move( component.data ) )
- {
- }
- Component& operator=( Component&& component ) throw( )
- {
- id = std::move( component.id );
- name = std::move( component.name );
- data = std::move( component.data );
- return ( *this );
- }
- uint32_t get_id( void ) const
- {
- return ( id );
- }
- private :
- uint32_t id;
- std::string name;
- std::vector< uint32_t > data;
- };
- // This object can be sorted
- inline bool operator<( const Component& component1, const Component& component2 )
- {
- return ( component1.get_id() < component2.get_id() );
- }
- int main( void )
- {
- /*******************************/
- /* Test vector insertion speed */
- /*******************************/
- std::vector< Component > vector;
- vector.reserve( COMPONENTS_COUNT + 1 );
- std::cout << "Push back components in the vector: ";
- auto startTime = std::chrono::steady_clock::now();
- // Back insertion
- for ( uint32_t i = 0; i < COMPONENTS_COUNT; ++i )
- {
- vector.push_back( Component( i + 1 ) );
- }
- auto thisTime = std::chrono::steady_clock::now();
- std::cout << std::chrono::duration_cast< std::chrono::milliseconds >( thisTime - startTime ).count() << "ms" << std::endl;
- std::cout << "Insert one component at the beginning of the vector: ";
- startTime = std::chrono::steady_clock::now();
- // Front insertion (all components are shifted)
- vector.insert( vector.begin(), Component( 0 ) );
- thisTime = std::chrono::steady_clock::now();
- std::cout << std::chrono::duration_cast< std::chrono::milliseconds >( thisTime - startTime ).count() << "ms" << std::endl;
- /*************************************/
- /* Test Boost vector insertion speed */
- /*************************************/
- boost::container::vector< Component > boost_vector;
- boost_vector.reserve( COMPONENTS_COUNT + 1 );
- std::cout << "Push back components in the Boost vector: ";
- startTime = std::chrono::steady_clock::now();
- // Back insertion
- for ( uint32_t i = 0; i < COMPONENTS_COUNT; ++i )
- {
- boost_vector.push_back( Component( i + 1 ) );
- }
- thisTime = std::chrono::steady_clock::now();
- std::cout << std::chrono::duration_cast< std::chrono::milliseconds >( thisTime - startTime ).count() << "ms" << std::endl;
- std::cout << "Insert one component at the beginning of the Boost vector: ";
- startTime = std::chrono::steady_clock::now();
- // Front insertion (all components are shifted)
- boost_vector.insert( boost_vector.begin(), Component( 0 ) );
- thisTime = std::chrono::steady_clock::now();
- std::cout << std::chrono::duration_cast< std::chrono::milliseconds >( thisTime - startTime ).count() << "ms" << std::endl;
- system( "PAUSE" );
- return ( 0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement