///////////////////////////////////////////////////////////////////////////// // temp.cpp - demonstrate performance implications of temporary objects // // Jim Fawcett, CSE691 - SW Modeling & Analysis, Fall 2000 ///////////////////////////////////////////////////////////////////////////// // Platform: Dell XPS1000, Windows 2000 Professional, SP1 #pragma warning(disable : 4786) #include #include #include "..\timer.h" using namespace std; ///////////////////////////////////////////////////////////////////////////// // class that creates a temporary when assigned a string class createTemp { public: createTemp() { } createTemp(string &s) : _s(s) { } createTemp& operator=(const createTemp &ct); private: string _s; }; createTemp& createTemp::operator =(const createTemp &ct) { if(this == &ct) return *this; _s = ct._s; return *this; } ///////////////////////////////////////////////////////////////////////////// // class that does not create a temporary when assigned a string class noTemp { public: noTemp() { } noTemp(string &s) : _s(s) { } noTemp& operator=(string &s); private: string _s; }; noTemp& noTemp::operator =(string &s) { if((void*)this == (void*)(&s)) return *this; _s = s; return *this; } // ///////////////////////////////////////////////////////////////////////////// // assign string to class object template void assign(T &t1, string &s) { t1 = s; } ///////////////////////////////////////////////////////////////////////////// // function to time function calls template clock_t timeFunction(void(*pFunc)(U u, V v), U u, V v, int N) { timer t; t.start(); int i; for(i=0; i(assign, ct, s, N); noTemp nt; timeFunction(assign, nt, s, N); cout << "\n\n"; }