Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <vector>
- #include <chrono>
- class A
- {
- public:
- A(){};
- virtual void test(int a, int b, int c) = 0;
- virtual void test2(unsigned int a, unsigned int b, unsigned c) = 0;
- uint8_t t{};
- };
- class B : public A
- {
- public:
- B()
- {
- t |= 0b00000001;
- }
- void test(int a, int b, int c) override
- {
- int test = a * b + c;
- }
- void test2(unsigned int a, unsigned int b, unsigned c) override
- {
- }
- };
- class C : public A
- {
- public:
- C()
- {
- t |= 0b00000010;
- }
- void test(int a, int b, int c) override
- {
- }
- void test2(unsigned int a, unsigned int b, unsigned c) override
- {
- unsigned int test = a * b + c;
- }
- };
- class D : public A
- {
- public:
- D()
- {
- t |= 0b00000011;
- }
- void test(int a, int b, int c) override
- {
- int test = a * b + c;
- }
- void test2(unsigned int a, unsigned int b, unsigned c) override
- {
- unsigned int test = a * b + c;
- }
- };
- int main()
- {
- std::vector<A*> tests{};
- int countB{}, countC{}, countD{};
- for (size_t i{}; i < 1000000; i++)
- {
- if (i%3 == 0)
- {
- countB++;
- tests.push_back(new B());
- }
- else if (i%2 == 0)
- {
- countC++;
- tests.push_back(new C{});
- }
- else
- {
- countD++;
- tests.push_back(new D());
- }
- }
- std::cout << "B classes: " << countB << ", C classes: " << countC << ", D classes: " << countD << "\n";
- int flagsWon{};
- int vFuncWon{};
- for (int i{}; i < 10000; i++)
- {
- int a{}, b{}, c{};
- std::chrono::time_point<std::chrono::steady_clock> timeBefore = std::chrono::high_resolution_clock::now();
- for (auto element : tests)
- {
- a += 2;
- b += 5;
- c += 9;
- if (element->t & 0b00000001)
- element->test(a, b, c);
- if (element->t & 0b00000010)
- element->test2(a, b, c);
- }
- std::chrono::time_point<std::chrono::steady_clock> timeAfter = std::chrono::high_resolution_clock::now();
- auto timeTwo = (timeAfter - timeBefore).count();
- //std::cout << "Flags :" << (timeAfter - timeBefore).count() << '\n';
- a = {}, b = {}, c = {};
- timeBefore = std::chrono::high_resolution_clock::now();
- for (auto element : tests)
- {
- a += 2;
- b += 5;
- c += 9;
- element->test(a, b, c);
- element->test2(a, b, c);
- }
- timeAfter = std::chrono::high_resolution_clock::now();
- //std::cout << "Ticks virtual pointer runs :" << (timeAfter - timeBefore).count() << '\n';
- auto timeoOne = (timeAfter - timeBefore).count();
- if(timeoOne < timeTwo)
- vFuncWon++;
- else
- flagsWon++;
- if(i%100 == 0)
- {
- std::cout << "intermediate results: " << '\n';
- std::cout << "Flags Won: " << flagsWon << '\n';
- std::cout << "VFunc Won: " << vFuncWon << '\n';
- }
- }
- std::cout << "Flags Won: " << flagsWon << '\n';
- std::cout << "VFunc Won: " << vFuncWon << '\n';
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement