I am profiling a C++ application compiled on optimization level -O3 with the intel c++ compiler from intel composer xe 2013. The profiler (Instruments on OS X) states that a very large portion of time is being spent calling the destructor for a particular type of object. However, it will not provide me with information regarding what function allocated the object in the first place. Is there any tool that can provide the information on what functions allocate the largest quantity of a certain type of object?

Edit: I have also tried the -profile-functions flag for the intel c++ compiler with no success.

有帮助吗?

解决方案

You could add two more parameters to the constructor, the file and the line number. Save that information in the object, and print it when the destructor is called. Optionally you could hide some of the ugliness in a macro for the constructor.

#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;
}

This is how it runs

$ g++ main.cpp -o main && ./main
dtor for object created in file: main.cpp line: 16
$
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top