performance - Delete a pointer to C++ list is very very slow. Why? -


i trying rid of stl list fast. have declared pointer list. manipulations , delete pointer free ram. process of deletion pointer list slow , slow when list.clear(). slow. why happen? how can delete allocated ram fast? when dealing vector , deque deletion fast. below program demonstrates that.

//============// // 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;  // main program  int main()  {   // variables , 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 output    cout << fixed;   cout << setprecision(5);   cout << showpoint;    // main bench loop    (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 vector      t1 = clock();      cout << "  1 --> build vector ..." << endl;      (i = 0; < 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 vector --> " << tall << endl;      // build list      t1 = clock();      cout << "  3 --> build list ..." << endl;      (i = 0; < 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 list   --> " << tall << endl;      // build deque      t1 = clock();      cout << "  5 --> build deque ..." << endl;      (i = 0; < 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 deque  --> " << tall << endl;      // sort vector      t1 = clock();      cout << "  7 --> sort vector ..." << endl;      sort(veca->begin(), veca->end());      t2 = clock();      tall = (t2-t1)/static_cast<double>(clocks_per_sec);      cout << "  8 --> done vector --> " << tall << endl;      // sort list      t1 = clock();      cout << "  9 --> sort list ..." << endl;      lista->sort();      t2 = clock();      tall = (t2-t1)/static_cast<double>(clocks_per_sec);      cout << " 10 --> done list   --> " << tall << endl;      // sort deque      t1 = clock();      cout << " 11 --> sort deque ..." << endl;      sort(deqa->begin(), deqa->end());      t2 = clock();      tall = (t2-t1)/static_cast<double>(clocks_per_sec);      cout << " 12 --> done deque  --> " << tall << endl;      // delete vector      t1 = clock();      cout << " 13 --> delete vector ..." << endl;      delete [] veca;      t2 = clock();      tall = (t2-t1)/static_cast<double>(clocks_per_sec);      cout << " 14 --> done vector --> " << tall << endl;      // delete list      t1 = clock();      cout << " 15 --> delete list ..." << endl;      delete [] lista;      t2 = clock();      tall = (t2-t1)/static_cast<double>(clocks_per_sec);      cout << " 16 --> done list   --> " << tall << endl;      t1 = clock();      // delete deque      cout << " 17 --> delete deque ..." << endl;      delete [] deqa;      t2 = clock();      tall = (t2-t1)/static_cast<double>(clocks_per_sec);      cout << " 18 --> done deque  --> " << tall << endl;   }    int sentinel;   cin >> sentinel;     return 0; } 

every element in list has own node, meaning allocation has freed.

if want rid of fast , use members trivial destructors (no call needed), use custom allocator list, optimized that.
btw: allocating container on heap pessimisation.

anyway, depending on use-case container std::vector might make sense instead.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -