Guest User

Untitled

a guest
Jun 16th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdint>
  2. #include <Windows.h>
  3. #include <iostream>
  4.  
  5. class Foo final
  6. {
  7. public:
  8.     Foo() {}
  9.     ~Foo() {}
  10.  
  11.     void Draw(char* arr, uint32_t ind)
  12.     {
  13.         arr[ind] = char(100);
  14.     }
  15. };
  16.  
  17. class Bar final
  18. {
  19. public:
  20.     Bar() {}
  21.     ~Bar() {}
  22.  
  23.     void Draw(char* arr, uint32_t ind)
  24.     {
  25.         arr[ind] = char(200);
  26.     }
  27. };
  28.  
  29. //===================================================================================================================================
  30. #define MODE_FOO 0
  31. #define MODE_BAR 1
  32.  
  33. union FooBar
  34. {
  35.     Foo* foo;
  36.     Bar* bar;
  37. };
  38.  
  39. void _DrawFoo(FooBar dr, char* arr, uint32_t ind)
  40. {
  41.     return dr.foo->Draw(arr, ind);
  42. }
  43.  
  44. void _DrawBar(FooBar dr, char* arr, uint32_t ind)
  45. {
  46.     return dr.bar->Draw(arr, ind);
  47. }
  48.  
  49. void(*__Draw)(FooBar dr, char* arr, uint32_t ind);
  50.  
  51. class Unvirtual
  52. {
  53. public:
  54.     Unvirtual(int mode)
  55.     {
  56.         switch(mode)
  57.         {
  58.         case 0:
  59.             mFooBar.foo = new Foo();
  60.             __Draw = _DrawFoo;
  61.             break;
  62.         default:
  63.             mFooBar.bar = new Bar();
  64.             __Draw = _DrawBar;
  65.             break;
  66.         }
  67.     }
  68.  
  69.     ~Unvirtual() {}
  70.  
  71.     void Draw(char* arr, uint32_t ind)
  72.     {
  73.         return __Draw(mFooBar, arr, ind);
  74.     }
  75.  
  76. private:
  77.     FooBar mFooBar;
  78. };
  79.  
  80. class Virtual
  81. {
  82. public:
  83.     Virtual() {}
  84.     ~Virtual() {}
  85.  
  86.     virtual void Draw(char*, uint32_t) = 0;
  87. };
  88.  
  89. class FooVirt: public Virtual
  90. {
  91. public:
  92.     FooVirt()
  93.     {
  94.         mFoo = new Foo();
  95.     }
  96.     ~FooVirt()
  97.     {
  98.         delete mFoo;
  99.     }
  100.  
  101.     void Draw(char* arr, uint32_t ind) override
  102.     {
  103.         return mFoo->Draw(arr, ind);
  104.     }
  105.  
  106. private:
  107.     Foo* mFoo;
  108. };
  109.  
  110. class BarVirt: public Virtual
  111. {
  112. public:
  113.     BarVirt()
  114.     {
  115.         mBar = new Bar();
  116.     }
  117.     ~BarVirt()
  118.     {
  119.         delete mBar;
  120.     }
  121.  
  122.     void Draw(char* arr, uint32_t ind) override
  123.     {
  124.         return mBar->Draw(arr, ind);
  125.     }
  126.  
  127. private:
  128.     Bar* mBar;
  129. };
  130.  
  131. int main()
  132. {
  133.     char *aaa = new char[1000000000];
  134.  
  135.     __int64 freq;
  136.     QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
  137.     __int64 start, end;
  138.  
  139.     Unvirtual unvirt(MODE_FOO);
  140.     QueryPerformanceCounter((LARGE_INTEGER*)&start);
  141.     for(uint32_t i = 0; i < 1000000000; i++)
  142.     {
  143.         unvirt.Draw(aaa, i);
  144.     }
  145.     QueryPerformanceCounter((LARGE_INTEGER*)&end);
  146.  
  147.     double t1 = (double)(end - start) / freq;
  148.  
  149.     Virtual* virt = new FooVirt();
  150.     QueryPerformanceCounter((LARGE_INTEGER*)&start);
  151.     for(uint32_t i = 0; i < 1000000000; i++)
  152.     {
  153.         virt->Draw(aaa, i);
  154.     }
  155.     QueryPerformanceCounter((LARGE_INTEGER*)&end);
  156.  
  157.     double t2 = (double)(end - start) / freq;
  158.  
  159.     std::cout << "Unvirtual time: " << t1 << std::endl;
  160.     std::cout << "Virtual time:   " << t2 << std::endl;
  161.     std::cin.get();
  162.  
  163.     delete[] aaa;
  164. }
Add Comment
Please, Sign In to add comment