我正在使用来自 intel composer xe 2013 的 intel c++ 编译器分析在优化级别 -O3 上编译的 C++ 应用程序。分析器(OS X 上的 Instruments)指出,大部分时间都花在调用析构函数上特定类型的对象。但是,它不会向我提供有关首先分配对象的函数的信息。是否有任何工具可以提供有关哪些功能分配了最大数量的某种类型对象的信息?
编辑:我也尝试了英特尔 C++ 编译器的 -profile-functions 标志,但没有成功。
最佳答案
您可以向构造函数添加另外两个参数,即文件和行号。将该信息保存在对象中,并在调用析构函数时打印它。您可以选择在构造函数的宏中隐藏一些丑陋之处。
#include <iostream>
#include <string>
using std::string;
class Object
{
string _file;
int _line;
public:
Object( const char * file, int line ) : _file(file), _line(line) {}
~Object() { std::cerr << "dtor for object created in file: " << _file << " line: " << _line << std::endl; }
};
int main( int argc, char * argv[] )
{
Object obj( __FILE__, __LINE__ );
return 0;
}
它是这样运行的
$ g++ main.cpp -o main && ./main
dtor for object created in file: main.cpp line: 16
$
关于c++ - 分析 C++ 析构函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22645201/