I just tried comparing std::sort with both std::vector<std::pair<float, unsigned int>> (filled with the push_back operation) and a simple array std::pair<float, unsigned int>> * (highlighted using new ones and then filled one by one). The comparison function simply compares the float parts of the pairs.
Surprisingly, when using 16M values ββon std :: vector it took only about 1940 ms, and in the array about 2190 ms. Can someone explain how a vector can be faster? Is it due to caching or is it just that the version of the std :: sort array is poorly implemented?
gcc (GCC) 4.4.5 20110214 (Red Hat 4.4.5-6)
Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz - cache size 8192 KB (the computer has two quad-core processors, but I assume that sorting is only single-threaded)
EDIT: Now you can call me dumbass, but when I tried to reproduce the code that I used for measurements (I already deleted the source), I can not reproduce the results - now the array version takes about 1915 + - 5 ms (measured for 32 runs) . I can only swear that three times (manually) I checked the test for 10 measurements with similar results, but this is not strict evidence.
There was probably an error in the source code, the background process seems unacceptable, because I have alternating measurements of vector and massive versions, and the results of the vector are saved and no user is logged in.
Please consider this question closed . Thanks for your efforts.
source share