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

标签 c++ macos profiling instruments

我第一次尝试在我的 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:

enter image description here

我以为我可以在 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 中产生以下结果:

enter image description here

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

enter image description here

出现一个对话窗口,您可以在其中输入参数。

关于c++ - macOS Time Profiler 分析 C++ 代码但找不到我的函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48035396/

相关文章:

python - 将 numpy 数组转换为字符串的最有效方法

c++ - 信号和槽的用途是什么

java - 如何检查在 YourKit 中记录对象分配期间创建了多少个类的实例?

c++ - 如何使表格的列可编辑?

swift - 如何在 OSX 上使用 Swift 获取蓝牙 MAC 地址

macos - 在 macOS 上的 MAMP 或 XAMPP 上安装 phalcon 时出现错误

java - 如何在 macOS 中卸载 Java JDK?

c++ - 无法使用 GetProcessMemoryInfo 测量静态数组内存使用情况

c++ - 使用 Live555 流式传输图像序列

c++ - 使 LLVM 无法包含 $(LLVM_SRC_ROOT)/Makefile.rules