基准测试中不同 "for" 的不同行为

different behavior for different "for"s in benchmark

我们可以使用 google benchmark or https://www.quick-bench.com/

编写一个简单的基准测试
static void range_based_for(benchmark::State &state) {
    for (auto _ : state) {
        std::to_string(__LINE__);
    }
}
BENCHMARK(range_based_for);

我们也可以改写成std::for_each,

static void std_for_each(benchmark::State &state) {
    std::for_each (std::begin(state), std::end(state), [] (auto _) {
        std::to_string(__LINE__);
    });
}
BENCHMARK(std_for_each);

一切都很好。 但是,当我们使用 old school for statement 时,它运行时没有完成。

static void old_school_for(benchmark::State &state) {
    for (auto iter = std::begin(state); iter != std::end(state); ++iter) {
        std::to_string(__LINE__);
    };
}
BENCHMARK(old_school_for);

其实std::for_each就是用这种风格实现的。他们的行为有何不同?

begin/end 函数记录在 a warning 中:说“不应直接调用这些函数”

These functions should not be called directly.

REQUIRES: The benchmark has not started running yet. Neither begin nor end have been called previously.

end calls StartKeepRunning

StartKeepRunning 是做什么的? It resets the number of iterations to the maximum

很明显,您只应调用 beginend 一次。第三个循环的不同之处在于 std::end(state) 每次迭代调用一次,这显然会将迭代计数重置为最大值。

我不知道为什么图书馆是这样设计的。