In C++ using STL which of the two operations take less CPU time to sort large distinct numbers:
- Push the elements into a set
- Store the elements in a vector and call sort() function?
In C++ using STL which of the two operations take less CPU time to sort large distinct numbers:
If it's a one-off operation, I would use a std::vector
followed by a std::sort
.
As far as asymptotic complexity is involved, the two solutions should be equivalent: insertion in a set is O(log(n)), and you do that for N elements, so it's O(N log(N)) (proof).
On the other hand, insertion in a vector (provided that you know the size in advance) is O(N), and sorting is O(N log(N)), so globally it's O(N log(N)).
But: a vector requires a one-off allocation (or, if you don't know the final size, it should reach the definitive size in O(log(N)) reallocations on typical implementations); on the other hand, a set, if implemented as an RB tree, requires an allocation for each node, which means that you have to call the allocator N times - and allocator calls, in a POD container, are probably going to be one of the bottlenecks. Also, a tree typically has less cache locality and uses more memory, so all this overhead is probably going to hurt performance.
Also, big-O notation shows the functional time dependency, but hides the multiplicative constants; don't take my word for this, but I'm almost sure that N*set insertion is going to cost more than a single sort at the end due to all the extra bookkeeping to do for each element (a tree insertion often needs some work to restore the RB tree properties).
On the other hand, if you have to keep your data structure sorted (with new data coming by the by) the set is usually the correct solution.
But, as always, when in doubt profile.