How to deal with a slow vector descriptor <string>?

Is there a known solution to the following problem:

  • You have a vector of a large number of lines

  • You happily fill it with several hundred thousand lines, and quickly

  • You arbitrarily manipulate your lines; life is good.

  • You are done with the vector; the vector is beyond the scope, and now you need to go get your coffee and sit back until each row is destroyed one by one.

Edit: problem solved!

I just ran the code below on Linux on the same computer, and everything was fine, and this led me to determine the solution. . It turned out that with my system - that I called me, a long time ago, but which I forgot.
After fixing the problem, the time was drastically reduced, even better than when using GCC!

This is a good puzzle though, instead of posting an answer, I will do something else:
I am not allowed to post generosity on this matter right now, but if you think you know the reason, take a picture. If it is right, I will accept it and give you a pleasant generosity. (Remind me if I forget to give you generosity!)
If no one knows, I'll just send it myself after a while.

Code example:

I was just as skeptical as everyone, but now I think people have a point when they are STL slow!
It took 3.95 seconds on my laptop: (critical shuffle)

#include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <string> #include <vector> int main() { using namespace std; srand((unsigned)time(NULL)); clock_t start; { vector<string> v(400000); for (size_t i = 0; i < v.size(); i++) { v[i].resize(16 + rand() % 32); // some variation } // shuffle for (size_t i = 0; i < (size_t)pow((double)v.size(), 1.15); i++) { size_t j = rand() * (RAND_MAX + 1) + rand(); swap(v[i % v.size()], v[j % v.size()]); } printf("Going out of scope...\n"); fflush(stdout); start = clock(); } clock_t end = clock(); printf("%u ms\n", (end - start) * 1000 / CLOCKS_PER_SEC); return 0; } 

It seems to me that this program uses the internal O (n 2 ) algorithm, either in Visual C ++ or in Windows. Not sure what is happening, but it is interesting ...

+7
source share
3 answers

Good, since no one understood this ...

This happened because heap tail checking was enabled on my system. After I deleted it, the code quickly exited.

+5
source

Use a custom valve with mass release.

+8
source

Why don't you create a dynamic vector so that you can control it with a smart reference counting pointer. Then you can make sure that the thread that has the last link to it is not a UI thread, so the UI thread is not the one that performs the processing when it goes out of scope.

You can even control the priority of the thread that is processing so that it drops and does not affect the rest of your threads β€” it will ensure that the user interface thread is scheduled ahead of the thread with a lower priority.

Note. I have never tried this, but I don’t understand why this will not work - but take the time to do it at your own peril and risk! :)

0
source

All Articles