Advertisement
Guest User

Visual Studio Vector implementation bug.

a guest
Apr 14th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <vector>
  4. #include <boost/container/vector.hpp>
  5. #include <windows.h>
  6.  
  7. #define COMPONENTS_COUNT 1000000
  8.  
  9. // Just a basic movable object for my tests
  10. class Component
  11. {
  12. public :
  13.     Component( void ) :
  14.         id( 0 ),
  15.         name( "default" ),
  16.         data( 100 )
  17.     {
  18.     }
  19.  
  20.     Component( uint32_t id ) :
  21.         id( id ),
  22.         name( "default" ),
  23.         data( 100 )
  24.     {
  25.     }
  26.  
  27.     Component( Component&& component ) throw( ) :
  28.         id( std::move( component.id ) ),
  29.         name( std::move( component.name ) ),
  30.         data( std::move( component.data ) )
  31.     {
  32.     }
  33.  
  34.     Component& operator=( Component&& component ) throw( )
  35.     {
  36.         id = std::move( component.id );
  37.         name = std::move( component.name );
  38.         data = std::move( component.data );
  39.         return ( *this );
  40.     }
  41.  
  42.     uint32_t get_id( void ) const
  43.     {
  44.         return ( id );
  45.     }
  46.  
  47. private :
  48.     uint32_t                id;
  49.     std::string             name;
  50.     std::vector< uint32_t > data;
  51. };
  52.  
  53. // This object can be sorted
  54. inline bool operator<( const Component& component1, const Component& component2 )
  55. {
  56.     return ( component1.get_id() < component2.get_id() );
  57. }
  58.  
  59. int main( void )
  60. {
  61.     /*******************************/
  62.     /* Test vector insertion speed */
  63.     /*******************************/
  64.     std::vector< Component > vector;
  65.  
  66.     vector.reserve( COMPONENTS_COUNT + 1 );
  67.  
  68.     std::cout << "Push back components in the vector: ";
  69.     auto startTime = std::chrono::steady_clock::now();
  70.  
  71.     // Back insertion
  72.     for ( uint32_t i = 0; i < COMPONENTS_COUNT; ++i )
  73.     {
  74.         vector.push_back( Component( i + 1 ) );
  75.     }
  76.  
  77.     auto thisTime = std::chrono::steady_clock::now();
  78.     std::cout << std::chrono::duration_cast< std::chrono::milliseconds >( thisTime - startTime ).count() << "ms" << std::endl;
  79.  
  80.     std::cout << "Insert one component at the beginning of the vector: ";
  81.     startTime = std::chrono::steady_clock::now();
  82.  
  83.     // Front insertion (all components are shifted)
  84.     vector.insert( vector.begin(), Component( 0 ) );
  85.  
  86.     thisTime = std::chrono::steady_clock::now();
  87.     std::cout << std::chrono::duration_cast< std::chrono::milliseconds >( thisTime - startTime ).count() << "ms" << std::endl;
  88.  
  89.     /*************************************/
  90.     /* Test Boost vector insertion speed */
  91.     /*************************************/
  92.     boost::container::vector< Component > boost_vector;
  93.  
  94.     boost_vector.reserve( COMPONENTS_COUNT + 1 );
  95.  
  96.     std::cout << "Push back components in the Boost vector: ";
  97.     startTime = std::chrono::steady_clock::now();
  98.  
  99.     // Back insertion
  100.     for ( uint32_t i = 0; i < COMPONENTS_COUNT; ++i )
  101.     {
  102.         boost_vector.push_back( Component( i + 1 ) );
  103.     }
  104.  
  105.     thisTime = std::chrono::steady_clock::now();
  106.     std::cout << std::chrono::duration_cast< std::chrono::milliseconds >( thisTime - startTime ).count() << "ms" << std::endl;
  107.  
  108.     std::cout << "Insert one component at the beginning of the Boost vector: ";
  109.     startTime = std::chrono::steady_clock::now();
  110.  
  111.     // Front insertion (all components are shifted)
  112.     boost_vector.insert( boost_vector.begin(), Component( 0 ) );
  113.  
  114.     thisTime = std::chrono::steady_clock::now();
  115.     std::cout << std::chrono::duration_cast< std::chrono::milliseconds >( thisTime - startTime ).count() << "ms" << std::endl;
  116.  
  117.     system( "PAUSE" );
  118.  
  119.     return ( 0 );
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement