macOS Time Profiler 分析 C++ 代码但找不到我的函数名称

macOS Time Profiler profiling c++ code but can't find my function name

我在 mac:

中第一次尝试使用 Time Profiler 时编写了一个简单的测试 C++ 代码
#include <iostream>     // std::cout

int frank() { return 0; }

int main () {
  int a = frank();
  std::cout << a << std::endl;
  return 0;
}

然后我编译了一下:

g++ test.cpp -o test0

然后我使用 Time Profile 来分析我的可执行文件 test0:

我以为我可以在 Call Tree 中找到我的函数 frank(),但我没有。

另一个问题是如何在 Time Profiler 中将参数传递给可执行文件。

Instruments 是一个采样分析器,其采样频率太低而无法检测到您对 frank() 的调用。要么在函数内部执行一些计算量大的计算,要么简单地调用它足够的次数。

将您的代码调整为以下 (C++11),其中在循环中调用稍微复杂的函数:

#include <iostream>     // std::cout

long frank(const unsigned int n) { return n*n; }

int main () {
    const auto nr_iterations = 1000000u;
    long a;
    for (auto n = 0u; n < nr_iterations; ++n)
    {
        a = frank(n);
    }
    std::cout << a << std::endl;
    return 0;
}

在我的机器上的 Instruments 中产生以下结果:

要通过 Instruments 传递参数,请单击记录按钮旁边的目标名称。在出现的上下文菜单中,单击“编辑目标”,其中“目标”是您的可执行文件的名称。例如:

会出现一个对话框 window,您可以在其中输入参数。