how best to ensure that pointer has been read into register, for benchmarking purposes?

59 Views Asked by At

I have some code roughly like this

unsafe fn foo(p: *const X) {
  let x = *p;
  let begin = Instant::now();
  ... // potentially expensive processing of x
  let duration = begin.elapsed();
}

The issue is that due to out-of-order execution, etc., duration will generally include the time spent waiting for p to be read from memory. I have reasons to want to benchmark the "potentially expensive processing" without including that waiting time. What can I do? Is this the place for memory barriers/fences? (note that I'm only concerned with single-threaded execution here)

0

There are 0 best solutions below