// C++ program to demonstrate the use of list containers #include #include #include #include #include using namespace std; class Timer { public: chrono::steady_clock::time_point start_time ; chrono::steady_clock::time_point end_time ; void startTime() { start_time = chrono::steady_clock::now() ; } void endTime() { end_time = chrono::steady_clock::now() ; } void printTime() { chrono::milliseconds elapsed_ms = chrono::duration_cast(end_time - start_time); cout << "Execution time: " << elapsed_ms.count() << " milliseconds" << endl; } }; //-------------------------------------------------------- int main() { // defining list Timer timerObject ; list list1 ; vector vec1 ; int noElements = 1000000 ; timerObject.startTime() ; for( int i1=1 ; i1 <= noElements ; i1++ ) { vec1.push_back( i1 ) ; } timerObject.endTime() ; cout << "Vector add elements to end: " << endl; timerObject.printTime() ; timerObject.startTime() ; for( int i1=1 ; i1 <= noElements ; i1++ ) { list1.push_back( i1 ) ; } timerObject.endTime() ; cout << "List add elements to end: " << endl; timerObject.printTime() ; //---------------------------------------------- int noElementsToDelete = 1000 ; timerObject.startTime() ; for( int i1=2 ; i1 <= noElementsToDelete ; i1++ ) { vec1.erase(vec1.begin() + 2) ; } timerObject.endTime() ; cout << "Vector delete elements: " << endl; timerObject.printTime() ; timerObject.startTime() ; list::iterator it = list1.begin(); for( int i1=2 ; i1 <= noElementsToDelete ; i1++ ) { advance(it, 1); list1.erase(it) ; } timerObject.endTime() ; cout << "List delete elements time: " << endl; timerObject.printTime() ; //---------------------------------------------- //Insert elements in the middle noElements = 1000 ; timerObject.startTime() ; for( int i1=1 ; i1 <= noElements ; i1++ ) { vec1.insert(vec1.begin() + 2, i1 ); ; } timerObject.endTime() ; cout << "Vector insert elements in middle: " << endl; timerObject.printTime() ; timerObject.startTime() ; list1.clear() ; list1.push_back( 1 ) ;list1.push_back( 2 ) ; it = list1.begin() ; advance(it, 2) ; for( int i1=1 ; i1 <= noElements ; i1++ ) { list1.insert( it , i1 ) ; } timerObject.endTime() ; cout << "List insert elements in middle: " << endl; timerObject.printTime() ; return 0; }