Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
virtual test
#include <iostream> #include <chrono> #include <vector> using namespace std; class B2{ public: virtual ~B2() = default; virtual int f(int n){return -n;} int g(int n){return -n;} }; class C : public B2{ public: int f(int n) override {return n;} }; int main() { std::vector<B2*> bs; const int numTest=1000000; for(int n=0;n<numTest;n++){ if(((double) rand() / (RAND_MAX))>0.5){ bs.push_back(new B2()); }else{ bs.push_back(new C()); } }; auto t1 = std::chrono::system_clock::now(); int s=0; for(int n=0;n<numTest;n++){ s+=bs[n]->f(n); }; auto t2= std::chrono::system_clock::now(); for(int n=0;n<numTest;n++){ s+=bs[n]->g(n); }; auto t3= std::chrono::system_clock::now(); auto t21=t2-t1; auto t32=t3-t2; std::cout<<t21.count()<<" "<<t32.count()<<" ratio="<< (((float)t21.count())/t32.count()) << std::endl; std::cout<<s<<std::endl; for(int n=0;n<numTest;n++){ delete bs[n]; }; }
run
|
edit
|
history
|
help
0
decltype_lambda
#21.2
fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
Adaptive return type
Regex success
Computing the factorial of an integer using factorial and iteration
assignment
Implements.cpp
hangman
operator new / delete