为什么将排序的键插入 std::set 比插入混洗的键快得多?

Leo*_*ruz 3 c++ stl red-black-tree stdset cache-locality

我意外地发现插入排序的键std::set比插入混洗的键要快得多。这有点违反直觉,因为一棵红黑树(我证实std::set作为自平衡二叉搜索树在我的系统上是作为红黑树实现的)需要做很多重新平衡操作来插入排序的序列键,因此插入排序的键应该比插入混洗的键花费更多的时间。

但事实是,插入排序的键比插入混洗的键快 15 倍!这是我的测试代码和一些结果:

#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <set>
#include <vector>
using namespace std;

int64_t insertion_time(const vector<int> &keys) {    
        auto start = chrono::system_clock::now();
        set<int>(keys.begin(), keys.end());
        auto stop = chrono::system_clock::now();
        auto elapsed = chrono::duration_cast<chrono::milliseconds>(stop - start);
        return elapsed.count(); 
}

int main() {
    size_t test_size;
    cout << "test size: ";
    cin >> test_size;
    vector<int> keys(test_size);
    for (int i = 0; i < test_size; ++i) {
        keys[i] = i;
    }
    
    // whether shuffled case or sorted case took first was irrelevant and results were similar
    auto rng = std::default_random_engine {};
    shuffle(keys.begin(), keys.end(), rng);
    cout << "shuffled: " << insertion_time(keys) << endl;

    sort(keys.begin(), keys.end());
    cout << "sorted: " << insertion_time(keys) << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)
// i7 8700, 32 GB RAM, WIN10 2004, g++ -O3 main.cpp
// An interesting observation is that the difference becomes larger as test_size being larger.
// Similar results showed up for my handwritten red-black tree and other
// machines( or other compilers, operating systems etc)

C:\Users\Leon\Desktop\testSetInsertion>a
test size: 1000000
shuffled: 585
sorted: 96

C:\Users\Leon\Desktop\testSetInsertion>a
test size: 3000000
shuffled: 2480
sorted: 296

C:\Users\Leon\Desktop\testSetInsertion>a
test size: 5000000
shuffled: 4805
sorted: 484

C:\Users\Leon\Desktop\testSetInsertion>a
test size: 10000000
shuffled: 11537
sorted: 977

C:\Users\Leon\Desktop\testSetInsertion>a
test size: 30000000
shuffled: 46239
sorted: 3076
Run Code Online (Sandbox Code Playgroud)

请问有人解释一下吗?我猜这与缓存位置有关,因为在插入排序键时,重新平衡通常涉及最近插入的那些节点。但以上只是我的猜测,我对缓存位置知之甚少。

Jar*_*d42 5

如果您查看https://en.cppreference.com/w/cpp/container/set/set

你可以看到:

复杂
[..]
2) N log(N),其中N = std::distance(first, last)在一般情况下,线性中N如果范围已经被value_comp排序()。

我们可以insert在循环中使用end()as 提示,它是具有正确提示的摊销常量。

  • 如果已排序,则只需进行一项检查即可找到新元素的位置(最后一个位置即可)。如果错误,则进行二分搜索(因此“log(N)”)来查找位置。 (2认同)