Guest User

Untitled

a guest
Jan 21st, 2019
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <sys/time.h>
  3. #include <string>
  4. #include <atomic>
  5. #include <functional>
  6.  
  7. size_t now() {
  8. struct timeval tv;
  9. gettimeofday(&tv, NULL);
  10. return tv.tv_sec * 1000000 + tv.tv_usec;
  11. }
  12.  
  13. class Long {
  14. public:
  15. void CtorAndDtor() {
  16. long a = 2334543523423423;
  17. }
  18.  
  19. void Copy() {
  20. long a = 234235345435345;
  21. long b = a;
  22. }
  23.  
  24. void Compare() {
  25. long a = 234454353453452;
  26. long b = 489123710234823;
  27. if (a == b) {
  28. return;
  29. }
  30. }
  31.  
  32. size_t Hash() {
  33. long a = 234234324324324;
  34. return a;
  35. }
  36. };
  37.  
  38. // all string is 16 length
  39. // it will not trigger malloc in +gcc5.1
  40. class String {
  41. public:
  42. void CtorAndDtor() {
  43. std::string a = "1234567890123456";
  44. }
  45.  
  46. void Copy() {
  47. std::string a = "1234567890123456";
  48. std::string b = a;
  49. }
  50.  
  51. void Compare() {
  52. std::string a = "1234567890123456";
  53. std::string b = "8973478163409324";
  54. if (a == b) {
  55. return;
  56. }
  57. }
  58.  
  59. size_t Hash() {
  60. std::string a = "1234567890123456";
  61. return std::hash<std::string>{}(a);
  62. }
  63. };
  64.  
  65. #define BENCH_MARK(TYPE, FUNC, LOOP_NUM, COST) do { \
  66. TYPE object; \
  67. auto begin = now(); \
  68. std::atomic_thread_fence(std::memory_order_acq_rel); \
  69. for (size_t i = 0; i < LOOP_NUM; i++) { \
  70. object.FUNC(); \
  71. } \
  72. std::atomic_thread_fence(std::memory_order_acq_rel); \
  73. auto end = now(); \
  74. COST = end - begin; \
  75. } while (0)
  76.  
  77. int main(int argc, char** argv) {
  78. size_t loop_num = atoi(argv[1]);
  79.  
  80. size_t Long_CtorAndDtor_Cost = 0;
  81. size_t String_CtorAndDtor_Cost = 0;
  82. BENCH_MARK(Long, CtorAndDtor, loop_num, Long_CtorAndDtor_Cost);
  83. BENCH_MARK(String, CtorAndDtor, loop_num, String_CtorAndDtor_Cost);
  84.  
  85. size_t Long_Copy_Cost = 0;
  86. size_t String_Copy_Cost = 0;
  87. BENCH_MARK(Long, Copy, loop_num, Long_Copy_Cost);
  88. BENCH_MARK(String, Copy, loop_num, String_Copy_Cost);
  89.  
  90. size_t Long_Compare_Cost = 0;
  91. size_t String_Compare_Cost = 0;
  92. BENCH_MARK(Long, Compare, loop_num, Long_Compare_Cost);
  93. BENCH_MARK(String, Compare, loop_num, String_Compare_Cost);
  94.  
  95. size_t Long_Hash_Cost = 0;
  96. size_t String_Hash_Cost = 0;
  97. BENCH_MARK(Long, Hash, loop_num, Long_Hash_Cost);
  98. BENCH_MARK(String, Hash, loop_num, String_Hash_Cost);
  99.  
  100. size_t CtorAndDtor_Ratio = Long_CtorAndDtor_Cost == 0 ? 0 : String_CtorAndDtor_Cost / Long_CtorAndDtor_Cost;
  101. std::cout << "CtorAndDtor Long(" << Long_CtorAndDtor_Cost << "us) -vs- String(" << String_CtorAndDtor_Cost << "us) " << CtorAndDtor_Ratio << std::endl;
  102.  
  103. size_t Long_Copy_Real = Long_Copy_Cost - Long_CtorAndDtor_Cost;
  104. size_t String_Copy_Real = String_Copy_Cost - String_CtorAndDtor_Cost;
  105. size_t Copy_Ratio = Long_Copy_Real == 0 ? 0 : String_Copy_Real / Long_Copy_Real;
  106. std::cout << "Copy Long(" << Long_Copy_Real << "us) -vs- String(" << String_Copy_Real << "us) " << Copy_Ratio << std::endl;
  107.  
  108. size_t Long_Compare_Real = Long_Compare_Cost - Long_CtorAndDtor_Cost;
  109. size_t String_Compare_Real = String_Compare_Cost - String_CtorAndDtor_Cost;
  110. size_t Compare_Ratio = Long_Compare_Real == 0 ? 0 : String_Compare_Real / Long_Compare_Real;
  111. std::cout << "Compare Long(" << Long_Compare_Real << "us) -vs- String(" << String_Compare_Real << "us) " << Compare_Ratio << std::endl;
  112.  
  113. size_t Long_Hash_Real = Long_Hash_Cost - Long_CtorAndDtor_Cost;
  114. size_t String_Hash_Real = String_Hash_Cost - String_CtorAndDtor_Cost;
  115. size_t Hash_Ratio = Long_Hash_Real == 0 ? 0 : String_Hash_Real / Long_Hash_Real;
  116. std::cout << "Hash Long(" << Long_Hash_Real << "us) -vs- String(" << String_Hash_Real << "us) " << Hash_Ratio << std::endl;
  117.  
  118. return 0;
  119. }
Add Comment
Please, Sign In to add comment