Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
make Derive final
#include <iostream> #include <cstdlib> #include <iostream> #include <chrono> #include <vector> class A{ public: int f1(){return randomNumber*3;}; public: virtual int f2(){return randomNumber*3;}; public: virtual int f3(){return randomNumber*3;}; public: int f4(){return f3();}; public: int randomNumber=((double) rand() / (RAND_MAX))*10; }; class B final : public A { public: virtual int f2() {return randomNumber*4;}; public: virtual int f3()final {return randomNumber*4;}; }; int main(){ std::vector<B*> bs; const int numTest=10000; for(int n=0;n<numTest;n++){ bs.push_back(new B()); }; int accu=0; for(int n=0;n<numTest;n++){ accu+=bs[n]->f1(); //warm }; auto t1= std::chrono::system_clock::now(); for(int n=0;n<numTest;n++){ accu+=bs[n]->f1(); //test 1 : base case, non virtual }; auto t2= std::chrono::system_clock::now(); for(int n=0;n<numTest;n++){ accu+=bs[n]->f2(); //test 2: virtual }; auto t3= std::chrono::system_clock::now(); for(int n=0;n<numTest;n++){ accu+=bs[n]->f3(); //test 3: virtual & final }; auto t4= std::chrono::system_clock::now(); for(int n=0;n<numTest;n++){ accu+=bs[n]->f4(); //test 4: virtual & final & encapsulator }; auto t5= std::chrono::system_clock::now(); auto t21=t2-t1; auto t32=t3-t2; auto t43=t4-t3; auto t54=t5-t4; std::cout<<"test1 base ="<<t21.count()<<std::endl; std::cout<<"test2 virtual ="<<t32.count()<<std::endl; std::cout<<"test3 virtual & final ="<<t43.count()<<std::endl; std::cout<<"test4 virtual & final & indirect="<<t54.count()<<std::endl; std::cout<<"forbid optimize"<<accu; }
run
|
edit
|
history
|
help
0
Replace all spaces in a string in C++
#26.fix
Thing v.2
OS_checker
-1 Girene Kadar Ortalama
#27
sample ptr and obj ex
1337
VC++
const reference life time extension