c++ - Should use an insertion sort or construct a heap to improve performance? -


itemprop = "text">

We have large (100,000+ elements) vent of structures (operator and lt; overload for providing ordering): < / P>

  std :: vector & lt; MyType & gt; VectorMyTypes; Std :: sort (vectorMyType.begin (), vectorMyType.end ());  

My problem is that while preserving the sort order, we are seeing performance issues while adding new elements of these vectors. At the moment we are doing something like this:

 for  (very large set) {vectorMyTypes.push_back (newType); Std :: sort (vectorMyType.begin (), vectorMyType.end ()); ... valid stuff (vector mitip); // This method assumes that vector can be ordered}  

it is not absolutely as I know that this example can be used in different ways However, it gives you an idea why performance can be a problem because I am sorting after every push_back . I think I have two options to improve performance:

  1. To improve sort performance (one hand is prepared?) entry

    std:

  2. Should I sort an inclusion? Is there anything in Boost that can help me here?

  3. Should I consider using a heap? How do i do this Edit:

    Thank you for all your responsibilities. I understand that the example I gave was far from optimal and it is not yet fully represented in my code. It was just that I was experiencing the lack of performance - that's why this question is not seeing many up-votes.)

    Many thanks for you, this is often The easiest answer is that which is the best, and perhaps it is an analysis of my problem which was blinding me for the most obvious solution. I like the preset method to insert directly into a pre-ordered vector.

    As I have commented, I am obliged to use vectors right now, so std :: set, std :: maps, etc. are not an option.

    No need to promote the entered entry:

      vectorMyTypes .inert (std :: upper_bound (vectorMyTypes.begin (), vector mitesapp.end (), new type), new type;  

    upper_bound provide a valid entry point As long as the vector has been sorted to begin, so long as you only insert the elements into your right place, I did basically do the lower_boun D was said, but if there are many equal elements in the vector, then upper_bound selects that entry point for which less work is required.

    This Copy the (n) elements, but you say that the inclusion sort is "indiscriminately fast", and it is fast if it is not fast enough, then you have to find a way to add items to batches and finally validate, Or leave the compatible storage and And switch to a container that maintains the order, such as set or multiset .

    A pile does not maintain the order in the underlying container, but the priority is good for the queue or the same, because it allows the maximum element to be removed faster, you say that you want to keep the vector in sequence , But if you do not actually recycle the entire collection anytime, you do not need to order it completely, and when a pile is useful.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -