0

I want to compare the performance of two algorithms on a large inputs. The primary difference between the algorithms is that one should have better cache access patterns than the other. I want the size of the input to be much larger than the L3 cache, so the input will exceed 64 MB.

The algorithms are implemented as func1 and func2. I would implement the test as

std::vector<int> input_1 = GetRandomInput();
std::vector<int> input_2 = input_1;

double func1_runtime = Benchmark(func1, input_1);
std::cout << "func1s runtime was " << func1_runtime << "\n";

double func2_runtime = Benchmark(func2, input_2);
std::cout << "func2s runtime was " << func2_runtime << "\n";

I'm trying to find a function Benchmark that will attempt to clear all of the data out of cache prior to running. Is there any such function in Google Benchmark?

Mark Wallace
  • 528
  • 2
  • 12
  • Doesn't `Benchmark(func1, input_1)` run func1 *multiple times*, to make the timed interval long enough to hide measurement noise and CPU / page-fault warm-up effects? If so, it wouldn't make sense to evict input_1 from cache just once before running it. But if you only run once, then see [Flushing the cache to prevent benchmarking fluctiations](https://stackoverflow.com/a/49077734) and [Is there a way to flush the entire CPU cache related to a program?](https://stackoverflow.com/a/48533834) – Peter Cordes Aug 04 '22 at 01:04
  • @PeterCordes I considered rolling my own, but I didn't understand step 3 in your first link, and hoped some library like Google Benchmark had solved this problem since 2018. – Mark Wallace Aug 04 '22 at 01:21
  • Step 3 is like `volatile void *sink = ptr;`, or especially `sink = ptr` with a global `sink` to make the variable obviously globally reachable. – Peter Cordes Aug 04 '22 at 01:25

0 Answers0