Advertisement
elvman

Untitled

May 16th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <functional>
  4.  
  5. using namespace std;
  6. using namespace std::chrono;
  7.  
  8. class Base
  9. {
  10. public:
  11. Base(){}
  12. virtual ~Base(){}
  13. virtual int func(int i) volatile = 0;
  14. };
  15.  
  16. class Derived : public Base
  17. {
  18. public:
  19. Derived(int base = 10) : base{base}
  20. {
  21.  
  22. }
  23. ~Derived(){}
  24.  
  25. virtual int func(int i) volatile
  26. {
  27. return i*base;
  28. }
  29. private:
  30. int base;
  31. };
  32.  
  33. struct Func
  34. {
  35. int base;
  36. int __attribute__ ((noinline)) operator()(int i)
  37. {
  38. return i*base;
  39. }
  40. Func(int base) : base {base}
  41. {
  42.  
  43. }
  44. };
  45. const int base = 10;
  46. int __attribute__ ((noinline)) calculate(int i)
  47. {
  48. return base*i;
  49. }
  50.  
  51. int main()
  52. {
  53. const int num = 6000;
  54. volatile Base *p = new Derived{10};
  55. int total = 0;
  56. auto start = high_resolution_clock::now();
  57. for (int i = 0; i < num; ++i)
  58. {
  59. total += p->func(i);
  60. }
  61. auto end = high_resolution_clock::now();
  62. std::cout<<"result: "<<total<<"\nvirtual call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl;
  63.  
  64. total = 0;
  65. start = high_resolution_clock::now();
  66. for (int i = 0; i < num; ++i)
  67. {
  68. total += calculate(i);
  69. }
  70. end = high_resolution_clock::now();
  71. std::cout<<"result: "<<total<<"\ndirect function call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl;
  72.  
  73. Func functor{10};
  74. total = 0;
  75. start = high_resolution_clock::now();
  76. for (int i = 0; i < num; ++i)
  77. {
  78. total += functor(i);
  79. }
  80. end = high_resolution_clock::now();
  81. std::cout<<"result: "<<total<<"\nfunctor call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl;
  82.  
  83. function<int(int, int)> lambda1 = [](int i, int base)
  84. {
  85. return i * base;
  86. };
  87. total = 0;
  88. start = high_resolution_clock::now();
  89. for (int i = 0; i < num; ++i)
  90. {
  91. total += lambda1(i, base);
  92. }
  93. end = high_resolution_clock::now();
  94. std::cout<<"result: "<<total<<"\nlambda1 call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl;
  95.  
  96. const int base = 10;
  97.  
  98. function<int(int)> lambda2 = [base](int i)
  99. {
  100. return i*base;
  101. };
  102. total = 0;
  103. start = high_resolution_clock::now();
  104. for (int i = 0; i < num; ++i)
  105. {
  106. total += lambda2(i);
  107. }
  108. end = high_resolution_clock::now();
  109. std::cout<<"result: "<<total<<"\nlambda2 call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl;
  110.  
  111.  
  112.  
  113. function<int(int)> bound = std::bind(calculate, std::placeholders::_1);
  114. total = 0;
  115. start = high_resolution_clock::now();
  116. for (int i = 0; i < num; ++i)
  117. {
  118. total += bound(i);
  119. }
  120. end = high_resolution_clock::now();
  121. std::cout<<"result: "<<total<<"\nbound call elapsed: \t"<<duration_cast<nanoseconds>(end-start).count()<<" nanoseconds.\n"<<std::endl;
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement