Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
base call virtual
#include <iostream> #include <cstdlib> #include <iostream> #include <chrono> #include <vector> class A{ public: int randomNumber=((double) rand() / (RAND_MAX))*10; public: virtual int f(){return randomNumber*3;}; public: virtual int g(){return randomNumber*3;}; public: int fa(){return randomNumber*3;}; }; class B final: public A { public: virtual int f(){return randomNumber*4;}; }; class C : public A { public: virtual int f()final{return randomNumber*4;}; public: virtual int g(){return randomNumber*5;}; }; int main(){ std::vector<A*> as; std::vector<B*> bs; std::vector<C*> cs; const int numTest=10000; for(int n=0;n<numTest;n++){ as.push_back(new A()); bs.push_back(new B()); cs.push_back(new C()); }; int accu=0; for(int n=0;n<numTest;n++){ accu+=as[n]->f(); //note: warm up }; auto t1= std::chrono::system_clock::now(); for(int n=0;n<numTest;n++){ accu+=as[n]->f(); //test 1 : base class - virtual function }; auto t2= std::chrono::system_clock::now(); for(int n=0;n<numTest;n++){ accu+=bs[n]->f(); //test 2: derived class (final at class-level) }; auto t3= std::chrono::system_clock::now(); for(int n=0;n<numTest;n++){ accu+=cs[n]->f(); //test 3: derived function (with final) }; auto t4= std::chrono::system_clock::now(); for(int n=0;n<numTest;n++){ accu+=cs[n]->g(); //test 4: derived function (w/o final) }; 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="<<t21.count()<<std::endl; std::cout<<"test2="<<t32.count()<<std::endl; std::cout<<"test3="<<t43.count()<<std::endl; std::cout<<"test4="<<t54.count()<<std::endl; std::cout<<"forbid optimize"<<accu; }
run
|
edit
|
history
|
help
0
binary
VS struct_member_name
Competitive - Algorithm for max number of superior characters
bool behaviour (convertion/promotion) for logical and artihmetic operators
typeid VC++
MSVC alias template
additional layer of indirection
Even Odd
-1 Girene Kadar Ortalama
#27