Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <celero/Celero.h>
- #include <string>
- CELERO_MAIN
- std::string name{"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod"
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
- "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo"
- "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"
- "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non"
- "proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
- "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod"
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
- "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo"
- "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"
- "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non"
- "proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
- "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod"
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
- "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo"
- "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"
- "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non"
- "proident, sunt in culpa qui officia deserunt mollit anim id est laborum."};
- BASELINE(CheckLetters, Branch, 10, 100000) {
- bool hasX = false;
- bool hasc = false;
- bool hasx = false;
- bool hase = false;
- for (const auto &x : name) {
- if (x == 'X')
- hasX |= true;
- else if (x == 'c')
- hasc |= true;
- else if (x == 'x')
- hasx |= true;
- else if (x == 'e')
- hase |= true;
- }
- fprintf(stderr, "%d %d %d %d\n", hasX, hasc, hasx, hase);
- }
- BENCHMARK(CheckLetters, Switch, 10, 100000) {
- bool hasX = false;
- bool hasc = false;
- bool hasx = false;
- bool hase = false;
- for (const auto &x : name) {
- switch (x) {
- case 'X':
- hasX |= true;
- break;
- case 'c':
- hasc |= true;
- break;
- case 'x':
- hasx |= true;
- break;
- case 'e':
- hase |= true;
- break;
- }
- }
- fprintf(stderr, "%d %d %d %d\n", hasX, hasc, hasx, hase);
- }
- BENCHMARK(CheckLetters, NoBranch, 10, 100000) {
- bool hasX = false;
- bool hasc = false;
- bool hasx = false;
- bool hase = false;
- for (const auto &x : name) {
- hasX |= (x == 'X');
- hasc |= (x == 'c');
- hasx |= (x == 'x');
- hase |= (x == 'e');
- }
- fprintf(stderr, "%d %d %d %d\n", hasX, hasc, hasx, hase);
- }
- BENCHMARK(CheckLetters, LookupTable, 10, 100000) {
- char chartab[0x100] = {0};
- for (const auto &x : name) {
- chartab[x] |= true;
- }
- fprintf(stderr, "%d %d %d %d\n", static_cast<bool>(chartab['X']),
- static_cast<bool>(chartab['c']), static_cast<bool>(chartab['x']),
- static_cast<bool>(chartab['e']));
- }
- /*
- ubuntu@ubuntu:~$ g++ -O2 -std=c++11 chardis.cpp -ICelero/include/ Celero/build/CMakeFiles/celero.dir/src/* -pthread
- ubuntu@ubuntu:~$ ./a.out 2> /dev/null
- Celero
- Timer resolution: 0.001000 us
- -----------------------------------------------------------------------------------------------------------------------------------------------
- Group | Experiment | Prob. Space | Samples | Iterations | Baseline | us/Iteration | Iterations/sec |
- -----------------------------------------------------------------------------------------------------------------------------------------------
- CheckLetters | Branch | Null | 10 | 100000 | 1.00000 | 2.66975 | 374566.91 |
- CheckLetters | Switch | Null | 10 | 100000 | 2.38431 | 6.36552 | 157096.36 |
- CheckLetters | NoBranch | Null | 10 | 100000 | 3.10831 | 8.29841 | 120505.01 |
- CheckLetters | LookupTable | Null | 10 | 100000 | 0.67333 | 1.79762 | 556291.10 |
- Complete.
- */
Advertisement
Add Comment
Please, Sign In to add comment