Add statistical code to container destructors in std header files. This does not require modification of a large amount of code for a large project. But this only shows the type of container (see my other answer here). The method does not require C ++ 0x or C ++ 11 or more.
The first and obligatory step is to add your std library under the source code, git, for example, to quickly see what has actually been changed, and to quickly switch between the changed and the original version.
Put this declaration of the Stat class in the sources folder of the std library:
class Stat { std::map<std::string,int> total; std::map<std::string,int> maximum; public: template<class T> int log( std::string cont, size_t size ) { std::string key = cont + ": " + typeid(T).name(); if( maximum[key] < size ) maximum[key] = size; total[key] += size; } void show_result() { std::cout << "container type total maximum" << std::endl; std::map<std::string,int>::const_iterator it; for( it = total.begin(); it != total.end(); ++it ) { std::cout << it->first << " " << it->second << " " << maximum[it->first] << std::endl; } } static Stat& instance(); ~Stat(){ show_result(); } };
Run a singleton instance of the Stat class in the cpp project file:
Stat& Stat::instance() { static Stat stat; return stat; }
Edit the std library container templates. Add statistics in destructors.
// modify this standart template library sources: template< T, Allocator = std::allocator<T> > vector { ... virtual ~vector() { Stat::instance().log<value_type>( "std::vector", this->size() ); } }; template< Key, T, Compare = std::less<Key>, Allocator = std::allocator<std::pair<const Key, T> > map { ... virtual ~map(){ Stat::instance().log<value_type>( "std::map", this->size() ); } };
Consider a program, for example:
int main() { { // reject to use C++0x, project does not need such dependency std_vector<int> v1; for(int i=0;i<10;++i) v1.push_back( i ); std_vector<int> v2; for(int i=0;i<10;++i) v2.push_back( i ); std_map<int,std::string> m1; for(int i=0;i<10;++i) m1[i]=""; std_map<int,std::string> m2; for(int i=0;i<20;++i) m2[i]=""; } Stat::instance().show_result(); return 0; }
Result for gcc:
container type total maximum std::map: St4pairIiSsE 30 20 std::vector: i 20 10
If you need a more detailed type description than finding information about your development environment. This conversion is described here for gcc: https://lists.gnu.org/archive/html/help-gplusplus/2009-02/msg00006.html
The conclusion could be like this:
container type total maximum std::map: std::pair<int, std::string> 30 20 std::vector: int 20 10