Guest User

benchmark

a guest
Sep 26th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include <celero/Celero.h>
  2. #include <string>
  3.  
  4. CELERO_MAIN
  5.  
  6. std::string name{"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod"
  7. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
  8. "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo"
  9. "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"
  10. "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non"
  11. "proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  12. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod"
  13. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
  14. "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo"
  15. "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"
  16. "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non"
  17. "proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  18. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod"
  19. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
  20. "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo"
  21. "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"
  22. "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non"
  23. "proident, sunt in culpa qui officia deserunt mollit anim id est laborum."};
  24.  
  25. BASELINE(CheckLetters, Branch, 10, 100000) {
  26.   bool hasX = false;
  27.   bool hasc = false;
  28.   bool hasx = false;
  29.   bool hase = false;
  30.  
  31.   for (const auto &x : name) {
  32.     if (x == 'X')
  33.       hasX |= true;
  34.     else if (x == 'c')
  35.       hasc |= true;
  36.     else if (x == 'x')
  37.       hasx |= true;
  38.     else if (x == 'e')
  39.       hase |= true;
  40.   }
  41.  
  42.   fprintf(stderr, "%d %d %d %d\n", hasX, hasc, hasx, hase);
  43. }
  44.  
  45. BENCHMARK(CheckLetters, Switch, 10, 100000) {
  46.   bool hasX = false;
  47.   bool hasc = false;
  48.   bool hasx = false;
  49.   bool hase = false;
  50.  
  51.   for (const auto &x : name) {
  52.     switch (x) {
  53.     case 'X':
  54.       hasX |= true;
  55.       break;
  56.     case 'c':
  57.       hasc |= true;
  58.       break;
  59.     case 'x':
  60.       hasx |= true;
  61.       break;
  62.     case 'e':
  63.       hase |= true;
  64.       break;
  65.     }
  66.   }
  67.  
  68.   fprintf(stderr, "%d %d %d %d\n", hasX, hasc, hasx, hase);
  69. }
  70.  
  71. BENCHMARK(CheckLetters, NoBranch, 10, 100000) {
  72.   bool hasX = false;
  73.   bool hasc = false;
  74.   bool hasx = false;
  75.   bool hase = false;
  76.  
  77.   for (const auto &x : name) {
  78.     hasX |= (x == 'X');
  79.     hasc |= (x == 'c');
  80.     hasx |= (x == 'x');
  81.     hase |= (x == 'e');
  82.   }
  83.  
  84.   fprintf(stderr, "%d %d %d %d\n", hasX, hasc, hasx, hase);
  85. }
  86.  
  87. BENCHMARK(CheckLetters, LookupTable, 10, 100000) {
  88.   char chartab[0x100] = {0};
  89.  
  90.   for (const auto &x : name) {
  91.     chartab[x] |= true;
  92.   }
  93.  
  94.   fprintf(stderr, "%d %d %d %d\n", static_cast<bool>(chartab['X']),
  95.           static_cast<bool>(chartab['c']), static_cast<bool>(chartab['x']),
  96.           static_cast<bool>(chartab['e']));
  97. }
  98.  
  99. /*
  100. ubuntu@ubuntu:~$ g++ -O2 -std=c++11 chardis.cpp -ICelero/include/ Celero/build/CMakeFiles/celero.dir/src/* -pthread
  101. ubuntu@ubuntu:~$ ./a.out 2> /dev/null
  102. Celero
  103. Timer resolution: 0.001000 us
  104. -----------------------------------------------------------------------------------------------------------------------------------------------
  105.      Group      |   Experiment    |   Prob. Space   |     Samples     |   Iterations    |    Baseline     |  us/Iteration   | Iterations/sec  |
  106. -----------------------------------------------------------------------------------------------------------------------------------------------
  107. CheckLetters    | Branch          | Null            |              10 |          100000 |         1.00000 |         2.66975 |       374566.91 |
  108. CheckLetters    | Switch          | Null            |              10 |          100000 |         2.38431 |         6.36552 |       157096.36 |
  109. CheckLetters    | NoBranch        | Null            |              10 |          100000 |         3.10831 |         8.29841 |       120505.01 |
  110. CheckLetters    | LookupTable     | Null            |              10 |          100000 |         0.67333 |         1.79762 |       556291.10 |
  111. Complete.
  112. */
Advertisement
Add Comment
Please, Sign In to add comment