gpt4 book ai didi

c++ - 删除指向 C++ 列表的指针非常非常慢。为什么?

转载 作者:行者123 更新时间:2023-11-30 04:06:01 40 4
gpt4 key购买 nike

我正试图快速摆脱 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;
}

最佳答案

列表中的每个元素都有自己的节点,这意味着必须释放额外的分配。

如果你想真正快速地摆脱它并使用具有普通析构函数的成员(不需要调用),请为列表使用自定义分配器,它为此进行了优化。
顺便说一句:在堆上分配容器是一种悲观。

无论如何,根据您的用例,另一个容器如 std::vector 可能更有意义。

关于c++ - 删除指向 C++ 列表的指针非常非常慢。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23064050/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com