| 1 | #include <algorithm>
|
|---|
| 2 | #include <array>
|
|---|
| 3 | #include <chrono>
|
|---|
| 4 | #include <iostream>
|
|---|
| 5 | #include <locale>
|
|---|
| 6 | #include <memory>
|
|---|
| 7 | #include <string>
|
|---|
| 8 | #include <vector>
|
|---|
| 9 |
|
|---|
| 10 | #include <cassert>
|
|---|
| 11 |
|
|---|
| 12 | struct __attribute__((aligned(64))) element {
|
|---|
| 13 | size_t value;
|
|---|
| 14 | };
|
|---|
| 15 |
|
|---|
| 16 | using block = std::array<element, 100>;
|
|---|
| 17 |
|
|---|
| 18 | block * create() {
|
|---|
| 19 | block * b = new block();
|
|---|
| 20 | for(auto & e : *b) {
|
|---|
| 21 | e.value = rand();
|
|---|
| 22 | }
|
|---|
| 23 | b->back().value = b->size();
|
|---|
| 24 |
|
|---|
| 25 | return b;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | static inline size_t find(const block & b) {
|
|---|
| 29 | size_t r = 0;
|
|---|
| 30 | for(; r < b.size(); r++) {
|
|---|
| 31 | if(__builtin_expect(b[r].value == b.size(), false)) break;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | return r;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | void usage(char * argv[]) {
|
|---|
| 38 | std::cerr << argv[0] << ": [DURATION (FLOAT:SEC)] [NBLOCKS]" << std::endl;;
|
|---|
| 39 | std::exit(1);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | int main(int argc, char * argv[]) {
|
|---|
| 43 | size_t nblocks = 1000;
|
|---|
| 44 | double duration = 5;
|
|---|
| 45 |
|
|---|
| 46 | std::cout.imbue(std::locale(""));
|
|---|
| 47 |
|
|---|
| 48 | switch (argc)
|
|---|
| 49 | {
|
|---|
| 50 | case 3:
|
|---|
| 51 | nblocks = std::stoul(argv[2]);
|
|---|
| 52 | [[fallthrough]];
|
|---|
| 53 | case 2:
|
|---|
| 54 | duration = std::stod(argv[1]);
|
|---|
| 55 | if( duration <= 0.0 ) {
|
|---|
| 56 | std::cerr << "Duration must be positive, was " << argv[1] << "(" << duration << ")" << std::endl;
|
|---|
| 57 | usage(argv);
|
|---|
| 58 | }
|
|---|
| 59 | [[fallthrough]];
|
|---|
| 60 | case 1:
|
|---|
| 61 | break;
|
|---|
| 62 | default:
|
|---|
| 63 | usage(argv);
|
|---|
| 64 | break;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | std::vector<std::unique_ptr<block>> blocks;
|
|---|
| 68 | for(size_t i = 0; i < nblocks; i++) {
|
|---|
| 69 | blocks.emplace_back( create() );
|
|---|
| 70 | }
|
|---|
| 71 | std::random_shuffle(blocks.begin(), blocks.end());
|
|---|
| 72 |
|
|---|
| 73 | size_t CRC = 0;
|
|---|
| 74 | size_t count = 0;
|
|---|
| 75 |
|
|---|
| 76 | using clock = std::chrono::high_resolution_clock;
|
|---|
| 77 | auto before = clock::now();
|
|---|
| 78 |
|
|---|
| 79 | while(true) {
|
|---|
| 80 | for(const auto & b : blocks) {
|
|---|
| 81 | CRC += find(*b);
|
|---|
| 82 | count++;
|
|---|
| 83 | }
|
|---|
| 84 | auto now = clock::now();
|
|---|
| 85 | std::chrono::duration<double> durr = now - before;
|
|---|
| 86 | if( durr.count() > duration ) {
|
|---|
| 87 | break;
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | auto after = clock::now();
|
|---|
| 92 | std::chrono::duration<double> durr = after - before;
|
|---|
| 93 | duration = durr.count();
|
|---|
| 94 |
|
|---|
| 95 | using std::chrono::duration_cast;
|
|---|
| 96 | using std::chrono::nanoseconds;
|
|---|
| 97 |
|
|---|
| 98 | size_t ops_sec = size_t(double(count) / duration);
|
|---|
| 99 | auto dur_nano = duration_cast<nanoseconds>(std::chrono::duration<double>(1.0)).count();
|
|---|
| 100 |
|
|---|
| 101 | std::cout << "CRC : " << CRC << "\n";
|
|---|
| 102 | std::cout << "Duration : " << duration << "s\n";
|
|---|
| 103 | std::cout << "Total ops : " << count << "\n";
|
|---|
| 104 | std::cout << "Ops/sec : " << ops_sec << "\n";
|
|---|
| 105 | std::cout << "ns/Op : " << ( dur_nano / ops_sec )<< "\n";
|
|---|
| 106 | }
|
|---|