C common template martial arts will be the first place:
Vector v.s. list v.s. degue
Original author: beyond_ml
In order to save time and space, the following program will test all items behind the system. The performance difference between the various contestants is then analyzed according to the specific results.
SequenceperFormance.cpp
//: c04: sequenceperformance.cpp
// Compaing the Performance of the Basic
// Sequence Containers for Various Operations
#include
#include
#include
#include
#include
#include
#include
#include
Using namespace std;
Class fixedsize
{
INT X [20];
// Automatic Generation of Default Construction,
// Copy-Constructor and operator =
} fs;
Template
Struct insertback
{
Void Operator () (Cont & C, Long Count)
{
For (long i = 0; i C.PUSH_BACK (FS); } Char * testname () {return "insertback"; } Template Struct insertfront { Void Operator () (Cont & C, Long Count) { Long CNT = count * 10; For (long i = 0; i C.PUSH_FRONT (FS); } Char * testname () {return "insertfront"; } Template Struct InsertMiddle { Void Operator () (Cont & C, Long Count) { TypeName Cont :: Iterator IT; Long CNT = count / 10; For (long i = 0; i { // must get the iterator every Time to Keep // from causing an access violation with // Vector. Increment it to put it in the // middle of the container: IT = c.begin (); IT ; C.INSERT (IT, FS); } } Char * testname () {return "insertmiddle"; } Template Struct randomaccess {// not for list Void Operator () (Cont & C, Long Count) { INT SZ = C.Size (); Long cnt = count * 100; for (long (long i = 0; i C [rand ()% sz]; } Char * testname () {return "randomaccess"; } Template Struct Traversal { Void Operator () (Cont & C, Long Count) { Long CNT = count / 100; For (long i = 0; i { TypenAme Cont :: item it = C.BEGIN (), End = c.end (); While (it! = end) IT ; } } Char * testname () {return "traversal"; } Template Struct swap { Void Operator () (Cont & C, Long Count) { Int middle = c.size () / 2; TypenAme Cont :: item it = C.BEGIN (), MID = C.BEGIN (); IT ; // put it in the middle For (int x = 0; x MID ; Long CNT = count * 10; For (long i = 0; i SWAP (* it, * mid); } Char * testname () {return "swap"; } Template Struct Removemiddle { Void Operator () (Cont & C, Long Count) { Long CNT = count / 10; IF (CNT> C.Size ()) { Cout << "Removemiddle: Not Enough Elements" << ENDL; Return; } For (long i = 0; i { TypeName Cont :: item. = c.begin (); IT ; C.RASE (IT); } } Char * testname () {return "removemiddle"; } Template Struct Removeback { Void Operator () (Cont & C, Long Count) { Long CNT = count * 10; IF (CNT> C.Size ()) { Cout << "Removeback: Not Enough Elements" << ENDL; Return; } For (long i = 0; i C.POP_BACK (); } Char * testname () {return "removeback"; } Template Void MeasureTime (OP F, Container & C, Long Count) { String ID (TypeID (f) .name ()); Bool Deque = id.find ("deque")! = string :: npos; Bool list = id.find ("list")! = string :: npos; Bool vector = id.find ("vector")! = string :: npos; String cont = Deque? "deque": list? "List" :? "Vector": "unknown"; Cout << f.testname () << "for" << Cont << ":" // Standard C Library CPU Ticks: Clock_t ticks = clock (); F (c, count); // Run Test Ticks = clock () - ticks; COUT << Ticks << Endl; } TypedEf Deque Typedef List TypedEf Vector Int main (int Argc, char * argv []) { SRAND (Time (0)); Long count = 1000; IF (argc> = 2) count = ATOI (Argv [1]); DF DEQ; LF LST; VF VEC, VECRES; VECRES.Rserve (count); // preallocate storage MeasureTime (INSERTBACK MeasureTime (INSERTBACK MeasureTime (INSERTBACK MeasureTime (INSERTBACK // can't push_front () with a vector: //! MeasureTime (INSERTFRONT MeasureTime (INSERTFRONT MeasureTime (INSERTFRONT MeasureTime (INSERTMIDDLE MeasureTime (INSERTMIDDLE MeasureTime (INSERTMIDDLE MeasureTime (randomaccess MeasureTime (Randomaccess // can't operator [] with a list: //! MeasureTime (Randomaccess MeasureTime (Traversal MeasureTime (Traversal MeasureTime (SWAP MeasureTime (SWAP MeasureTime (SWAP MeasureTime (RemoveMiddle MeasureTime (RemoveMiddle MeasureTime (RemoveMiddle VEC.Resize (vec.size () * 10); // make it bigger MeasureTime (Removeback MeasureTime (RemoveBack MeasureTime (RemoveBack } ///: ~ The fourth game is inserted forward Vector abstains. He does not support Push_Front operations. Test Results: INSERTFRONT for Deque: 20000 Insertfront for list: 30000 DEQUE wins. BEYOND_ML Comments: Not unexpected, Deque's perspective skills are of course. The fifth bureau insertion Test Results: InsertMiddle for Vector: 40000 INSERTMIDDLE for Deque: 0 INSERTMIDDLE for List: 0 Beyond_ml Comments: It is difficult for Vector, in any case, Vector is not suitable for intermediate insertion. At the same time, I want to sing a praise for Deque, I can make a flat hand with List, I really can't afford. Sixth Bureau exchange data Test Results: Swap for vector: 0 Swap for Deque: 10000 Swap for list: 2000 Beyond_ml Comments: The cluster advantage of Vector is very suitable for memory exchange. Seventh place to delete Test Results: Removemiddle for Vector: 50000 RemoveMiddle for Deque: 0 Removemiddle for list: 0 Beyond_ml Comment: It is difficult to make a vector, in any case, Vector is also not suitable for intermediate deletion. At the same time, I have to sing a praise for Deque, and List is a flat hand, I am really great. Rear part of the eighth game Test Results: Removeback for Vector: 0 REMOVEBACK for Deque: 0 Removeback for List: 20000 Beyond_ml Comments: Cheers for Vector and Deque! Very good! . Come to a summary. Game Project / Party Vector Deque List Memory Management Poor Good Perfect Use [] and AT () Operation Access Data Very Good Normal N / A Iterator Access Speed Good Very Good Push_Back Action (After insert) Good Good Good Push_Front Action (Before Insert) N / a VERY GOOD GOOD INSERT (Intermediate Insert) Perfect Perfect ERASE Poor Perfect Perfect Pop_Back (Rear Details) Perfect Perfect Normal Swap (Excluding Data) Perfect Very Good Good Traversing Perfect Good Normal Oh, seems to end In fact, there is not, we still have a lot of things! For example, when using the vector, we can preserve enough space in advance, and the efficiency of use will be improved! In addition, they are not the same as the design, and each of them has their own unique extinction. If you let them get full, your program will come to a grade. Let us work together.