G2A Many GEOs
SHARE
TWEET

Virtual Functions vs Flags

a guest Sep 18th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <vector>
  4. #include <chrono>
  5.  
  6. class A
  7. {
  8. public:
  9.     A(){};
  10.     virtual void test(int a, int b, int c) = 0;
  11.     virtual void test2(unsigned int a, unsigned int b, unsigned c) = 0;
  12.  
  13.     uint8_t t{};
  14. };
  15.  
  16. class B : public A
  17. {
  18. public:
  19.     B()
  20.     {
  21.         t |= 0b00000001;
  22.     }
  23.     void test(int a, int b, int c) override
  24.     {
  25.         int test = a * b + c;
  26.     }
  27.     void test2(unsigned int a, unsigned int b, unsigned c) override
  28.     {
  29.        
  30.     }
  31. };
  32.  
  33. class C : public A
  34. {
  35. public:
  36.     C()
  37.     {
  38.         t |= 0b00000010;
  39.     }
  40.     void test(int a, int b, int c) override
  41.     {
  42.     }
  43.     void test2(unsigned int a, unsigned int b, unsigned c) override
  44.     {
  45.         unsigned int test = a * b + c;
  46.     }
  47. };
  48.  
  49.  
  50. class D : public A
  51. {
  52. public:
  53.     D()
  54.     {
  55.         t |= 0b00000011;
  56.     }
  57.     void test(int a, int b, int c) override
  58.     {
  59.         int test = a * b + c;
  60.     }
  61.     void test2(unsigned int a, unsigned int b, unsigned c) override
  62.     {
  63.         unsigned int test = a * b + c;
  64.     }
  65. };
  66.  
  67. int main()
  68. {
  69.     std::vector<A*> tests{};
  70.     int countB{}, countC{}, countD{};
  71.     for (size_t i{}; i < 1000000; i++)
  72.     {
  73.         if (i%3 == 0)
  74.         {
  75.             countB++;
  76.             tests.push_back(new B());
  77.         }
  78.         else if (i%2 == 0)
  79.         {
  80.             countC++;
  81.             tests.push_back(new C{});
  82.         }
  83.         else
  84.         {
  85.             countD++;
  86.             tests.push_back(new D());
  87.         }
  88.     }
  89.     std::cout << "B classes: " << countB << ", C classes: " << countC << ", D classes: " << countD << "\n";
  90.  
  91.     int flagsWon{};
  92.     int vFuncWon{};
  93.     for (int i{}; i < 10000; i++)
  94.     {
  95.  
  96.         int a{}, b{}, c{};
  97.         std::chrono::time_point<std::chrono::steady_clock> timeBefore = std::chrono::high_resolution_clock::now();
  98.  
  99.         for (auto element : tests)
  100.         {
  101.             a += 2;
  102.             b += 5;
  103.             c += 9;
  104.             if (element->t & 0b00000001)
  105.                 element->test(a, b, c);
  106.             if (element->t & 0b00000010)
  107.                 element->test2(a, b, c);
  108.         }
  109.         std::chrono::time_point<std::chrono::steady_clock> timeAfter = std::chrono::high_resolution_clock::now();
  110.  
  111.         auto timeTwo = (timeAfter - timeBefore).count();
  112.         //std::cout << "Flags :" << (timeAfter - timeBefore).count() << '\n';
  113.        
  114.         a = {}, b = {}, c = {};
  115.         timeBefore = std::chrono::high_resolution_clock::now();
  116.         for (auto element : tests)
  117.         {
  118.             a += 2;
  119.             b += 5;
  120.             c += 9;
  121.             element->test(a, b, c);
  122.             element->test2(a, b, c);
  123.         }
  124.         timeAfter = std::chrono::high_resolution_clock::now();
  125.         //std::cout << "Ticks virtual pointer runs :" << (timeAfter - timeBefore).count() << '\n';
  126.         auto timeoOne = (timeAfter - timeBefore).count();
  127.         if(timeoOne < timeTwo)
  128.             vFuncWon++;
  129.         else
  130.             flagsWon++;
  131.  
  132.         if(i%100 == 0)
  133.         {
  134.             std::cout << "intermediate results: " << '\n';
  135.             std::cout << "Flags Won: " << flagsWon << '\n';
  136.             std::cout << "VFunc Won: " << vFuncWon << '\n';
  137.         }
  138.     }
  139.  
  140.     std::cout << "Flags Won: " << flagsWon << '\n';
  141.     std::cout << "VFunc Won: " << vFuncWon << '\n';
  142.  
  143.     system("pause");
  144. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top