Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 删除指向C+的指针+;这个列表非常慢。为什么?_C++_Performance_List_Pointers_Delete Operator - Fatal编程技术网

C++ 删除指向C+的指针+;这个列表非常慢。为什么?

C++ 删除指向C+的指针+;这个列表非常慢。为什么?,c++,performance,list,pointers,delete-operator,C++,Performance,List,Pointers,Delete Operator,我正试图尽快摆脱STL列表。所以我声明了一个指向该列表的指针。 我执行所有操作,然后删除指针以释放RAM。 但是删除列表指针的过程很慢,就像我删除list.clear()一样慢。所以它非常慢。为什么会这样?如何快速删除分配的RAM?当我处理vector和deque时,删除速度很快。下面是一个程序,它演示了这一点 //============// // STL delete // //============// #include <iostream> #include &

我正试图尽快摆脱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;
}
//============//
//STL删除//
//============//
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用std::cout;
使用std::cin;
使用std::endl;
使用std::list;
使用std::vector;
使用std::deque;
使用std::fixed;
使用std::setprecision;
使用std::showpoint;
使用std::sort;
//主程序
int main()
{
//变量和参数
const long int I_MAX=静态施法(功率(10.0,7.5));
const long int K_MAX=静态施法(功率(10.0,6.0));
long int i;
长整型k;
时钟t1;
时钟t2;
双高;
//设置输出

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

如果您想很快地将其全部清除,并使用具有琐碎析构函数的成员(无需调用),请为列表使用自定义分配器,该分配器为此进行了优化。
顺便说一句:在堆上分配容器是一种悲观


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

问题不在于删除列表,而在于理解列表如何表示为堆分配的节点链

因此,基本上,当您删除列表时,它还必须删除~30M节点,这绝对是一个明显缓慢的操作

一般来说,对于小型内置类型来说,列表并不是一个很好的容器,因为节点开销可能比数据本身占用更多的空间


您能给我们提供更多关于您试图解决的实际问题的信息吗?

列表中的每个节点都在内存中的一个随机位置。在向量中,整个块都在一个点上,这对于一个deque来说也非常适用。性能问题应该伴随着编译器、编译器版本和优化设置。否则,我们将不知道您是否正在为优化构建计时,不知道您是否正在为编译器使用损坏的STL实现(可能已经修复)我在GNU g++4.8.1、Linux下的英特尔icpc 14.0以及Windows下的GNU g++4.8.1、英特尔icpc 14.0和VS 2013中遇到了相同的行为。我使用的是-O3标志。我回来的时间几乎相同…@paulsepolia:在这种情况下,标准就足够了。无需引用具体的实现。谢谢!我将尝试开发一个客户机om分配器。重要的一点是分配巨大的块,并尝试一次性消除所有的块。如果可能的话,使分配器的释放函数成为一个不可操作的函数是一个巨大的速度胜利。只希望你不要太快用完内存。我还能在哪里分配容器?在堆栈上,就像任何正常的行为良好的变量一样。在上面的pr上Gram我怎么能做到这一点?到目前为止,我并没有试图解决任何特定的问题。我只是对STL容器在某些操作下的行为获得了一些经验。顺便说一句,我只是注意到,如果我不对列表进行排序,但如果我在构建列表后不修改它,那么删除速度与deque相当!!因此,当然这与必须删除的额外数据量(节点)有关,但我还缺少一些东西…@paulsepolia,这听起来像是实现能够执行一些优化,这是通过重新排列节点来抑制的。