benchmark unaligned memory access

Nitsan Wakart nitsanw at yahoo.com
Thu Mar 5 11:32:36 UTC 2015


This isn't really a JMH issue.Unaligned memory access is not necessarily more expensive, depends on CPU model. The cost difference on recent intel is pretty small unless you read/write on the cache line boundary.I also suspect the cost of the load is eclipsed by the cost of the implicit Blackhole.consume() in your particular benchmark, you can try and find other ways to avoid DCE.
 

     On Thursday, March 5, 2015 1:08 PM, YC Huang <ychuang_cn at hotmail.com> wrote:
   

 Hi,
I'm trying to benchmark what's the cost between aligned/unaligned memory access,but my test-case doesn't show there's obvious performance difference when running on my laptop(Pentium 6200). Am I doing something wrong in the Test? Thank you...
BR,YC Huang
following is the test code:
@State(Scope.Thread)public class MemoryAccessTest {    private long alignedAddr, unalignedAddr;
    @Param({ "24" })    public int structSize;
    public static Unsafe unsafe;    static {        try {            Field field = Unsafe.class.getDeclaredField("theUnsafe");            field.setAccessible(true);            unsafe = (Unsafe) field.get(null);        } catch (Exception e) {        }    }
    @Setup    public void init() {        alignedAddr = unsafe.allocateMemory(structSize);        assert alignedAddr % 8 == 0;
        unalignedAddr = alignedAddr + 9;//    }
    @Benchmark    public long alignedMemAccess() {        return unsafe.getLong(alignedAddr);    }
    @Benchmark    public long unalignedMemAccess() {        return unsafe.getLong(unalignedAddr);    }}                         

   


More information about the jmh-dev mailing list