为什么一个简单的C++程序会产生这么多分支命令?在 Linux 上使用 perf

Why does a simple C++ program generate so many branch commands? Using perf on Linux

我一定是在做一些愚蠢的事情或者 perf 使用不正确?

#include <iostream>

int main()
{
        return 0;
}

编译命令(使用g++-9.2.1)

g++ -std=c++17 -Wall -Wextra -pedantic -O3 Source.cpp -o prog

紧随 tutorial

stat Run a command and gather performance counter statistics

我试过了

perf stat ./prog

并在输出中

       560,957      branches                  #  303.607 M/sec
        16,181      branch-misses             #    2.88% of all branches

问题是为什么?我应该在 运行 这个命令之前“清理”寄存器吗?这正常吗?

大约 80% 的分支来自动态链接。需要打开文件,然后需要解析动态库。这需要做出大量决策,因为必须测试文件的内容以查看其格式、包含哪些部分等等。

剩下的 20% 中的大部分正是在可执行文件上运行的相同类型的逻辑。它具有复杂的格式,代码必须解析该格式以找出它有哪些部分,找到每个部分的结尾,并在程序开始执行之前决定如何将它们放置在内存中。