下面是STL的测试情况:
every 3 seconds add/del element number is 40000
stl list has 40000 elements
add cost time too much: 109
stl list has 0 elements
delete cost time too much: 4992
***************************************************
stl list has 40000 elements
add cost time too much: 499
stl list has 0 elements
delete cost time too much: 4961
***************************************************
stl list has 40000 elements
add cost time too much: 499
stl list has 0 elements
delete cost time too much: 4961
***************************************************
stl list has 40000 elements
add cost time too much: 499
stl list has 0 elements
delete cost time too much: 4961
***************************************************
stl list has 40000 elements
add cost time too much: 483
stl list has 0 elements
delete cost time too much: 4945
***************************************************
下面使用自己写的链表:
every 3 seconds add/del element number is 40000
my list has 40000 elements
add cost time too much: 110
my list has 0 elements
delete cost time too much: 10218
***************************************************
my list has 40000 elements
add cost time too much: 578
my list has 0 elements
delete cost time too much: 10920
***************************************************
my list has 40000 elements
add cost time too much: 452
my list has 0 elements
delete cost time too much: 11232
***************************************************
my list has 40000 elements
add cost time too much: 2496
my list has 0 elements
delete cost time too much: 12043
***************************************************
my list has 40000 elements
add cost time too much: 577
my list has 0 elements
delete cost time too much: 11295
***************************************************
my list has 40000 elements
add cost time too much: 468
my list has 0 elements
delete cost time too much: 11716
***************************************************
my list has 40000 elements
add cost time too much: 2449
my list has 0 elements
delete cost time too much: 12059
***************************************************
my list has 40000 elements
add cost time too much: 437
my list has 0 elements
delete cost time too much: 12090
***************************************************
my list has 40000 elements
add cost time too much: 436
my list has 0 elements
delete cost time too much: 12527
***************************************************
my list has 40000 elements
add cost time too much: 499
my list has 0 elements
delete cost time too much: 12792
***************************************************
my list has 40000 elements
add cost time too much: 452
my list has 0 elements
delete cost time too much: 12215
***************************************************
my list has 40000 elements
add cost time too much: 421
my list has 0 elements
delete cost time too much: 11981
***************************************************
my list has 40000 elements
add cost time too much: 468
my list has 0 elements
delete cost time too much: 12261
***************************************************
my list has 40000 elements
add cost time too much: 453
完整代码如下:
// TestList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <Windows.h> #include <list> #include<algorithm> #include <time.h> #include <iostream> using namespace std; //待测试的对象,链表中的每个元素就是对象A的指针 class A {}; //每3秒钟插入链表末尾/从链表首部取出的元素个数 int testPressureNum = 40000; //测试的STL链表 list<A*> testList; //自己写的链表 typedef struct { A* p; void* prev; void* next; } SelfListElement; SelfListElement* myListHead; SelfListElement* myListTail; int myListSize; //向自己写的链表首部添加元素 bool add(A* packet) { SelfListElement* ele = new SelfListElement; ele->p = packet; myListSize++; if (myListHead == NULL) { myListHead = myListTail = ele; ele->prev = NULL; ele->next = NULL; return true; } ele->next = myListHead; myListHead->prev = ele; ele->prev = NULL; myListHead = ele; return true; } // 从自己写的链表尾部取出元素 SelfListElement* get() { if (myListTail == NULL) return NULL; myListSize--; SelfListElement* p = myListTail; if (myListTail->prev == NULL) { myListHead = myListTail = NULL; } else { myListTail = (SelfListElement*)myListTail->prev; myListTail->next = NULL; } return p; } //从STL链表中取出元素并删除 struct DeletePointer { template<typename T> void operator()( T* ptr ) const { delete ptr; } }; void testDelete1() { /* while (testList.size() > 0) { A* p = testList.back(); testList.pop_back(); delete p; p = NULL; } */ std::for_each(testList.begin(),testList.end(),DeletePointer()); testList.resize(0); } //从简单链表中取出元素并删除 void testDelete2() { do { SelfListElement* packet = myListTail; if (packet == NULL) break; packet = get(); delete packet->p; delete packet; packet = NULL; } while (true); } //向Stl链表中添加元素 void testAdd1() { for (int i = 0; i < testPressureNum; ++i) { testList.push_back(new A()); } } //向简单链表中添加元素 void testAdd2() { for (int i = 0; i < testPressureNum; i++) { A* p = new A(); add(p); } } void printUsage(int argc, _TCHAR* argv[]) { cout<<"usage: "<<argv[0]<<" [1|2] [oneRoundPressueNum]"<<endl <<"1 means STL, 2 means simple list\noneRoundPressueNum means in 3 seconds how many elements add/del in list"<<endl; } int _tmain(int argc, _TCHAR* argv[]) { //为方便测试可使用2个参数 if (argc < 2) { printUsage(argc, argv); return -1; } int type = _wtoi(argv[1]); if (type != 1 && type != 2) { printUsage(argc, argv); return -2; } if (argc > 2) testPressureNum = _wtoi(argv[2]); cout<<"every 3 seconds add/del element number is "<<testPressureNum<<endl; time_t tStart = time(NULL); while (true) { DWORD dwAdd = GetTickCount(); if (type == 1) { testAdd1(); cout<<"stl list has "<<testList.size()<<" elements"<<endl; } else { testAdd2(); cout<<"my list has "<<myListSize<<" elements"<<endl; } dwAdd = GetTickCount() - dwAdd; cout<<"add cost time too much: "<<dwAdd<<endl; DWORD dwDel = GetTickCount(); if (type == 1) { testDelete1(); cout<<"stl list has "<<testList.size()<<" elements"<<endl; } else { testDelete2(); cout<<"my list has "<<myListSize<<" elements"<<endl; } dwDel = GetTickCount() - dwDel; cout<<"delete cost time too much: "<<dwDel<<endl; cout<<endl; cout<<"***************************************************"<<endl; cout<<endl; //每3秒一个周期 Sleep(3000); } return 0; }