///////////////////////////////////////////////////////////////////////////// // temp2.cpp - demonstrate performance implications of temporary objects // one more time // // Jim Fawcett, ELE591/791 - Intelligent Systems, Summer 2000 ///////////////////////////////////////////////////////////////////////////// #pragma warning(disable : 4786) #include #include #include #include #include "..\timer.h" using namespace std; ///////////////////////////////////////////////////////////////////////////// // populate collection of elements template void popCollection(cont &c, elem e1, elem e2, elem e3, int N) { c.insert(c.begin(),e1); for(int i=0; i elem FindElem1(cont c, elem e) { static elem nullElem; static bool first = true; for(cont::iterator i = c.begin(); i != c.end(); i++) { if(*i == e) { if(first) { cout << "\n -- found " << (*i); first = false; } return *i; } } return nullElem; } ///////////////////////////////////////////////////////////////////////////// // another function to find an element in a collection of elements template elem& FindElem2(cont &c, elem &e) { static elem nullElem; static bool first = true; for(cont::iterator i = c.begin(); i != c.end(); i++) { if(*i == e) { if(first) { cout << "\n -- found " << (*i); first = false; } return *i; } } return nullElem; } ///////////////////////////////////////////////////////////////////////////// // another function to find an element in a collection of elements template elem& FindElem3(cont &c, elem &e) { static elem nullElem; static bool first = true; cont::iterator end(c.end()); for(cont::iterator i = c.begin(); i != end; ++i) { if(*i == e) { if(first) { cout << "\n -- found " << (*i); first = false; } return *i; } } return nullElem; } ///////////////////////////////////////////////////////////////////////////// // still another function to find an element in a collection of elements template elem& FindElem4(cont &c, elem &e) { static elem nullElem; static bool first = true; cont::iterator i(find(c.begin(), c.end(), e)); if(i == c.end()) return nullElem; else { if(first) { cout << "\n -- found " << (*i); first = false; } return *i; } return nullElem; } // ///////////////////////////////////////////////////////////////////////////// // function to time function calls template clock_t timeFunction(U(*pFunc)(V v, W w), V v, W w, int N) { timer t; t.start(); int i; for(i=0; i names; string elem1 = "Groucho", elem2 = "Harpo", elem3 = "Zeppo"; popCollection(names,elem1,elem2,elem3,Nfill); const int Nrun = 20000; timeFunction, string>(FindElem1,names,elem3,Nrun); timeFunction&, string&> (FindElem2,names,elem3,Nrun); timeFunction&, string&> (FindElem3,names,elem3,Nrun); timeFunction&, string&> (FindElem4,names,elem3,Nrun); cout << "\n\n"; }