“坑爹的LIST容器--删除操作千倍消耗CPU”一文有误,在VC2010里是STL完胜
来源:互联网 发布:看图软件 安卓版 编辑:程序博客网 时间:2024/05/21 17:34
下面是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; }
- “坑爹的LIST容器--删除操作千倍消耗CPU”一文有误,在VC2010里是STL完胜
- STL容器删除操作
- STL中各种容器的删除操作
- STL中容器的删除操作
- STL中各种容器的删除操作
- STL容器的遍历删除操作erase
- STL容器操作比较(一):vector,deque,list
- STL总结(一)顺序容器的操作
- STL容器删除操作总结
- STL容器删除操作总结
- 《STL源码剖析》---list容器insert操作的个人理解
- 使用STL中的list容器实现单链表的操作
- STL里的容器区别
- STL list容器的使用
- STL List容器的使用
- stl容器在循环遍历中删除元素的方式
- STL容器:在遍历的过程中删除元素
- STL容器元素的删除
- python脚本自动监视tomcat进程,如果崩溃就自动重启
- 第五章 代码块和迭代器
- 一些关于分布式技术的资源
- 7.1.1除法
- 从朋友的签名上看来的,觉得很有意义……“三帮三不帮原则”
- “坑爹的LIST容器--删除操作千倍消耗CPU”一文有误,在VC2010里是STL完胜
- 玩具语言LLL之Lex
- UVA 10382 Watering Grass(区间嵌套)
- 第六章 表达式
- Android游戏编程之SurfaceView进行连续渲染
- HDU_2026 首写字母变大写
- 第七章 方法
- Oracle锁机制
- A Lightweight Logger for C++