我正试图快速摆脱STL列表。所以我已经声明了一个指向该列表的指针。 我做所有操作,然后删除指针以释放RAM。 但删除指向列表的指针的过程很慢,并且与list.clear()时一样慢。所以它很慢。为什么会这样?如何快速删除分配的RAM?当我处理vector和deque时,删除速度很快。以下是一个演示该程序的程序。
//============//
// STL delete //
//============//
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <deque>
#include <cmath>
#include <iomanip>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
using std::list;
using std::vector;
using std::deque;
using std::fixed;
using std::setprecision;
using std::showpoint;
using std::sort;
// the main program
int main()
{
// variables and parameters
const long int I_MAX = static_cast<long int>(pow(10.0, 7.5));
const long int K_MAX = static_cast<long int>(pow(10.0, 6.0));
long int i;
long int k;
clock_t t1;
clock_t t2;
double tall;
// set the output
cout << fixed;
cout << setprecision(5);
cout << showpoint;
// main bench loop
for (k = 0; k < K_MAX; k++)
{
list<double> * listA = new list<double> [1];
vector<double> * vecA = new vector<double> [1];
deque<double> * deqA = new deque<double> [1];
cout << endl;
cout << "------------------------------->>> " << k << endl;
cout << endl;
// build the vector
t1 = clock();
cout << " 1 --> build the vector ..." << endl;
for (i = 0; i < I_MAX; i++)
{ vecA->push_back(static_cast<double>(cos(i))); }
t2 = clock();
tall = (t2-t1)/static_cast<double>(CLOCKS_PER_SEC);
cout << " 2 --> done with the vector --> " << tall << endl;
// build the list
t1 = clock();
cout << " 3 --> build the list ..." << endl;
for (i = 0; i < I_MAX; i++)
{ listA->push_back(static_cast<double>(cos(i))); }
t2 = clock();
tall = (t2-t1)/static_cast<double>(CLOCKS_PER_SEC);
cout << " 4 --> done with the list --> " << tall << endl;
// build the deque
t1 = clock();
cout << " 5 --> build the deque ..." << endl;
for (i = 0; i < I_MAX; i++)
{ deqA->push_back(static_cast<double>(cos(i))); }
t2 = clock();
tall = (t2-t1)/static_cast<double>(CLOCKS_PER_SEC);
cout << " 6 --> done with the deque --> " << tall << endl;
// sort the vector
t1 = clock();
cout << " 7 --> sort the vector ..." << endl;
sort(vecA->begin(), vecA->end());
t2 = clock();
tall = (t2-t1)/static_cast<double>(CLOCKS_PER_SEC);
cout << " 8 --> done with the vector --> " << tall << endl;
// sort the list
t1 = clock();
cout << " 9 --> sort the list ..." << endl;
listA->sort();
t2 = clock();
tall = (t2-t1)/static_cast<double>(CLOCKS_PER_SEC);
cout << " 10 --> done with the list --> " << tall << endl;
// sort the deque
t1 = clock();
cout << " 11 --> sort the deque ..." << endl;
sort(deqA->begin(), deqA->end());
t2 = clock();
tall = (t2-t1)/static_cast<double>(CLOCKS_PER_SEC);
cout << " 12 --> done with the deque --> " << tall << endl;
// delete the vector
t1 = clock();
cout << " 13 --> delete the vector ..." << endl;
delete [] vecA;
t2 = clock();
tall = (t2-t1)/static_cast<double>(CLOCKS_PER_SEC);
cout << " 14 --> done with the vector --> " << tall << endl;
// delete the list
t1 = clock();
cout << " 15 --> delete the list ..." << endl;
delete [] listA;
t2 = clock();
tall = (t2-t1)/static_cast<double>(CLOCKS_PER_SEC);
cout << " 16 --> done with the list --> " << tall << endl;
t1 = clock();
// delete the deque
cout << " 17 --> delete the deque ..." << endl;
delete [] deqA;
t2 = clock();
tall = (t2-t1)/static_cast<double>(CLOCKS_PER_SEC);
cout << " 18 --> done with the deque --> " << tall << endl;
}
int sentinel;
cin >> sentinel;
return 0;
}
答案 0 :(得分:2)
列表中的每个元素都有自己的节点,这意味着必须释放额外的分配。
如果你想要快速摆脱它并使用具有微不足道的析构函数的成员(不需要调用),请为列表使用自定义分配器,该分配器针对此进行了优化。
BTW:在堆上分配容器是一种悲观。
无论如何,根据您的使用情况,另一个容器如std::vector
可能会有意义。
答案 1 :(得分:0)
问题不在于删除列表,而是了解列表如何表示为堆分配的节点链。
所以基本上当你删除列表时,它也必须删除~30M节点,这绝对是一个明显缓慢的操作。
一般来说,由于节点开销可能比数据本身占用更多空间,因此列表对于小型内置类型来说不是一个很好的容器。
您能否提供有关您尝试解决的真正问题的更多信息?