///////////////////////////////////////////////////////////////////////////// // constr.cpp - demonstrate performance implications of object creation // // Jim Fawcett, CSE691 - SW Modeling & Analysis, Fall 2000 ///////////////////////////////////////////////////////////////////////////// // Platform: Dell XPS1000, Windows 2000 Professional, SP1 #include #include #include "..\timer.h" using namespace std; ///////////////////////////////////////////////////////////////////////////// // very small class class empty { }; ///////////////////////////////////////////////////////////////////////////// // bigger class - holds an array of chars class strArray { public: strArray(char *s); private: char ch[100]; }; strArray::strArray(char *s) { for(int i=0; i<100 && s[i] != '\0'; i++) ch[i] = s[i]; } ///////////////////////////////////////////////////////////////////////////// // a class holding several different data types (defined above etc) class strMembers { public: strMembers(char *s); private: empty e; strArray sa; string ss; }; strMembers::strMembers(char *s) : sa(s), ss(s) { } // ///////////////////////////////////////////////////////////////////////////// // build local empty object void buildEmpty() { static bool first = true; if(first) { cout << "\n buildEmpty()"; first = false; } empty e; } ///////////////////////////////////////////////////////////////////////////// // build local strArray object void buildStrArray() { static bool first = true; if(first) { cout << "\n buildStrArray()"; first = false; } strArray sa("this is a string"); } ///////////////////////////////////////////////////////////////////////////// // build local string object void buildString() { static bool first = true; if(first) { cout << "\n buildString()"; first = false; } string ss("this is a string"); } ///////////////////////////////////////////////////////////////////////////// // build local strMembers object void buildStrMembers() { static bool first = true; if(first) { cout << "\n buildStrMembers()"; first = false; } strMembers sm("this is a string"); } // ///////////////////////////////////////////////////////////////////////////// // function to time function calls template clock_t timeFunction(U(*pFunc)(), int N) { timer t; t.start(); int i; for(i=0; i(buildEmpty, N); Ttest = timeFunction(buildStrArray, N); Ttest = timeFunction(buildString, N); Ttest = timeFunction(buildStrMembers, N); cout << "\n\n"; }