Generate a vector of indices such as following the indices describes a path over the whole vector. The path is randomized to avoid triggering automatic prefetching in the CPU.
| 34 | // a path over the whole vector. The path is randomized to avoid triggering |
| 35 | // automatic prefetching in the CPU. |
| 36 | std::vector<int32_t> RandomPath(int32_t size) { |
| 37 | std::default_random_engine gen(42); |
| 38 | std::vector<int32_t> indices(size); |
| 39 | |
| 40 | for (int32_t i = 0; i < size; ++i) { |
| 41 | indices[i] = i; |
| 42 | } |
| 43 | std::shuffle(indices.begin(), indices.end(), gen); |
| 44 | std::vector<int32_t> path(size, -999999); |
| 45 | int32_t prev; |
| 46 | prev = indices[size - 1]; |
| 47 | for (int32_t i = 0; i < size; ++i) { |
| 48 | int32_t next = indices[i]; |
| 49 | path[prev] = next; |
| 50 | prev = next; |
| 51 | } |
| 52 | return path; |
| 53 | } |
| 54 | |
| 55 | // Cache / main memory latency, depending on the working set size |
| 56 | static void memory_latency(benchmark::State& state) { |
no test coverage detected