// #include <stream.h> #include <stdlib.h> #include <time.h> #define N 2500 #define N2 10000000 static int allocated; /** * Class RandNum * * Provides random numbers based on the second-order linear * congruential generator that we've always used. Initialization * constants are suggested by J. G. Skellam. */ class RandNum { // The current value of the random number generator. public: int *currval; /** * constructor ** */ // The constructor merely initializes the current value. RandNum() { currval = new int[2]; // Allocate array. currval[0] = 13; currval[1] = 117; } /** * next * * Returns the next random number in the sequence. */ int next() { int temp; // Holds intermediate calculations. temp = (currval[0] * 254754 + currval[1] * 529562) % 999563; currval[1] = currval[0]; currval[0] = temp; return (temp); } /** * nextwc * * Returns next random number, but with a ceiling. */ int nextwc(int ceiling) { return(next() % ceiling); } /** * abs_nextwc * * Returns next random number with ceiling; applies absolute value * function on the way out. */ int abs_nextwc(int ceiling) { int temp; temp = nextwc(ceiling); return (abs(temp)); } }; // class testclass { public: char *bytes; testclass(int n) { bytes = new char[n]; allocated++; } ~testclass() { delete bytes; allocated--; } }; // class testclass1 { public: int func(int t) { t=t+1; return(t); } }; // class testclassr { public: int a; virtual int func(int t) { t=t+2; return(t); } }; // class testclass2: public testclassr { public: virtual int func(int t) { t=t+1; return(t); } }; int main (void) { int i,k,r; unsigned long time; int which; RandNum rndnum; testclass *test[N]; int msecs = CLOCKS_PER_SEC/1000; int total_time; testclassr *tt0; testclass1 *tt1; testclass2 *tt2; time = clock(); // allocated=0; for(k=0;k<1;k++) { for(i=0;i<N;i++) test[i] = new testclass((int)rndnum.abs_nextwc(5000)); cout<<"created "<<i<<" objects in "<<(clock()-time)/msecs<<" msecs"<<" allocated: "<<allocated<<"\n"; time = clock(); for(i=0;i<N;i++) delete test[i]; cout<<"deleted "<<i<<" objects in "<<(clock()-time)/msecs<<"msecs "<<" allocated: "<<allocated<<"\n"; } cout<<"--------------------------------------\n"; allocated=0; for(i=0;i<N;i++) test[i]=NULL; // total_time=0; for(k=0;k<10;k++) { time = clock(); for(i=0;i<N;i++) { which = (int)rndnum.abs_nextwc(N); if(test[which]!=NULL) delete test[which]; test[which] = new testclass((int)rndnum.abs_nextwc(5000)); } cout<<" created "<<i<<" objects in "<<(clock()-time)/msecs<<"msecs "<<" allocated: "<<allocated<<"\n"; total_time+=(clock()-time)/msecs; } cout<<" created "<<i*k<<" objects in "<<total_time<<"msecs\n"; // r=0; tt0 = new testclassr; time = clock(); for(i=0;i<N2;i++) r+=tt0->func(0); cout<<N2<<" virtual functions in "<<(clock()-time)/msecs<<"msecs, result: "<<r<<"\n"; r=0; tt2 = new testclass2; time = clock(); for(i=0;i<N2;i++) r+=tt2->func(0); cout<<N2<<" virtual functions in "<<(clock()-time)/msecs<<"msecs, result: "<<r<<"\n"; r=0; tt1 = new testclass1; time = clock(); for(i=0;i<N2;i++) r+=tt1->func(0); cout<<N2<<" functions in "<<(clock()-time)/msecs<<"msecs, result: "<<r<<"\n"; } //