微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么反向迭代器与 std::reverse 相比如此慢然后迭代?

如何解决为什么反向迭代器与 std::reverse 相比如此慢然后迭代?

#include <iostream>
#include <vector>
#include <windows.h>

using namespace std;

int v_size = 100000;

void foo1()
{
    vector<int>test(v_size,1);
    auto start = GetTickCount();
    reverse(test.begin(),test.end());
    int h = 0;
    for (auto& i : test)
    {
        i += h;
        ++h;
    }
    reverse(test.begin(),test.end());
    auto end = GetTickCount();
    cout <<"foo1 time consumption: "<< (end - start) << endl;
}
void foo2()
{
    vector<int>test(v_size,1);
    auto start = GetTickCount();
    int h = 0;
    for (auto it = test.rbegin(); it != test.rend(); ++it)
    {
        *it += h;
        ++h;
    }
    auto end = GetTickCount();
    cout << "foo2 time consumption : " << (end - start) << endl;
}


int main()
{
    foo1();
    foo2();
    foo2();
    foo1();
    foo2();
}

结果是:

foo1 time consumption: 0
foo2 time consumption : 156
foo2 time consumption : 156
foo1 time consumption: 0
foo2 time consumption : 171

我注意到两件事:

首先,如果我用普通的 for 循环替换 range for 循环,它会变慢。但是时间消耗还是40ms左右。

其次,反向迭代器太慢了。为什么?

解决方法

基准测试非常困难。 See this talk by Chandler Carruth。 我建议使用一个好的基准测试套件,例如Google Benchmark

您可以使用 Quick Bench 在线运行您的 Google 基准测试。所以只需使用以下代码尝试一下:

#include <benchmark/benchmark.h>
#include <vector>

static constexpr int v_size = 100000;

static void foo1(benchmark::State& state) {
    std::vector<int>test(v_size,1);
    for (auto _ : state) {
        reverse(test.begin(),test.end());
        int h = 0;
        for (auto& i : test) {
            i += h;
            ++h;
        }
        reverse(test.begin(),test.end());
    }
    benchmark::DoNotOptimize(test);
}
BENCHMARK(foo1);

void foo2(benchmark::State& state)
{
    std::vector<int>test(v_size,1);
    for (auto _ : state) {
        int h = 0;
        for (auto it = test.rbegin(); it != test.rend(); ++it) {
            *it += h;
            ++h;
        }
    }
    benchmark::DoNotOptimize(test);
}
BENCHMARK(foo2);

您会发现使用反向迭代器比使用反向迭代器快 3.7 倍(两次)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。