Created
January 28, 2020 07:00
-
-
Save chuntaro/90e128e6df735db406739f4f4c483b70 to your computer and use it in GitHub Desktop.
仮想継承時の仮想関数呼び出しのベンチマーク
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| chuntaro@NTEmacs64 windows-nt ~ | |
| $ g++ -O3 -Wall -Wextra test.cpp | |
| chuntaro@NTEmacs64 windows-nt ~ | |
| $ ./a.exe | |
| 500000000 | |
| 810ms | |
| 1000000000 | |
| 1746ms | |
| chuntaro@NTEmacs64 windows-nt ~ | |
| $ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <chrono> | |
| #include "test.hpp" | |
| using namespace std; | |
| int main() | |
| { | |
| // A* a = new D(); // コンパイルエラー | |
| A* a = static_cast<B*>(new D()); | |
| A* va = new vD(); | |
| auto s = chrono::system_clock::now(); | |
| for (int i = 0; i < 1'000'000'000; ++i) { | |
| a->inc(i); | |
| } | |
| auto e = chrono::system_clock::now(); | |
| cout << a->a << endl; | |
| cout << chrono::duration_cast<chrono::milliseconds>(e - s).count() << "ms" << endl; | |
| s = chrono::system_clock::now(); | |
| for (int i = 0; i < 1'000'000'000; ++i) { | |
| va->inc(i); | |
| } | |
| e = chrono::system_clock::now(); | |
| cout << va->a << endl; | |
| cout << chrono::duration_cast<chrono::milliseconds>(e - s).count() << "ms" << endl; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef TEST_HPP | |
| #define TEST_HPP | |
| class A { | |
| public: | |
| int a = 0; | |
| virtual void inc(int) { | |
| ++a; | |
| }; | |
| }; | |
| class B : public A { | |
| public: | |
| virtual void inc(int r) { | |
| A::inc(r); | |
| }; | |
| }; | |
| class C : public A { | |
| public: | |
| virtual void inc(int r) { | |
| A::inc(r); | |
| }; | |
| }; | |
| class vB : virtual public A { | |
| public: | |
| virtual void inc(int r) { | |
| A::inc(r); | |
| }; | |
| }; | |
| class vC : virtual public A { | |
| public: | |
| virtual void inc(int r) { | |
| A::inc(r); | |
| }; | |
| }; | |
| class D : public B, public C { | |
| public: | |
| virtual void inc(int r) { | |
| if (r & 1) { | |
| B::inc(r); | |
| } else { | |
| C::inc(r); | |
| } | |
| }; | |
| }; | |
| class vD : public vB, public vC { | |
| public: | |
| virtual void inc(int r) { | |
| if (r & 1) { | |
| vB::inc(r); | |
| } else { | |
| vC::inc(r); | |
| } | |
| }; | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment