java bytebuffer读取_java – 直接ByteBuffer相对于绝对读取性能

这篇博客探讨了Java中使用ByteBuffer进行直接读取与相对读取时的性能差异。作者通过JMH基准测试发现,绝对读取的性能大约是相对读取的两倍。测试在JDK 1.8和JDK 9上进行了验证,虽然JDK 9的性能有所提升,但两种读取方式的性能差距依然明显。
摘要由CSDN通过智能技术生成

当我测试直接java.nio.ByteBuffer的读取性能时,我注意到绝对读取的平均速度是相对读数的两倍.另外,如果我比较相对于绝对读取的源代码,除了相对读维护和内部计数器之外,代码几乎相同.我不知道为什么我会看到速度有如此巨大的差异?

以下是我的JMH基准测试的源代码:

public class DirectByteBufferReadBenchmark {

private static final int OBJ_SIZE = 8 + 4 + 1;

private static final int NUM_ELEM = 10_000_000;

@State(Scope.Benchmark)

public static class Data {

private ByteBuffer directByteBuffer;

@Setup

public void setup() {

directByteBuffer = ByteBuffer.allocateDirect(OBJ_SIZE * NUM_ELEM);

for (int i = 0; i < NUM_ELEM; i++) {

directByteBuffer.putLong(i);

directByteBuffer.putInt(i);

directByteBuffer.put((byte) (i & 1));

}

}

}

@Benchmark

@BenchmarkMode(Mode.Throughput)

@OutputTimeUnit(TimeUnit.SECONDS)

public long testReadAbsolute(Data d) throws InterruptedException {

long val = 0l;

for (int i = 0; i < NUM_ELEM; i++) {

int index = OBJ_SIZE * i;

val += d.directByteBuffer.getLong(index);

d.directByteBuffer.getInt(index + 8);

d.directByteBuffer.get(index + 12);

}

return val;

}

@Benchmark

@BenchmarkMode(Mode.Throughput)

@OutputTimeUnit(TimeUnit.SECONDS)

public long testReadRelative(Data d) throws InterruptedException {

d.directByteBuffer.rewind();

long val = 0l;

for (int i = 0; i < NUM_ELEM; i++) {

val += d.directByteBuffer.getLong();

d.directByteBuffer.getInt();

d.directByteBuffer.get();

}

return val;

}

public static void main(String[] args) throws Exception {

Options opt = new OptionsBuilder()

.include(DirectByteBufferReadBenchmark.class.getSimpleName())

.warmupIterations(5)

.measurementIterations(5)

.forks(3)

.threads(1)

.build();

new Runner(opt).run();

}

}

这些是我的基准测试的结果:

Benchmark Mode Cnt Score Error Units

DirectByteBufferReadBenchmark.testReadAbsolute thrpt 15 88.605 ± 9.276 ops/s

DirectByteBufferReadBenchmark.testReadRelative thrpt 15 42.904 ± 3.018 ops/s

该测试是在MacbookPro(2.2GHz Intel Core i7,16Gb DDR3)和JDK 1.8.0_73上运行的.

UPDATE

我用JDK 9-ea b134进行相同的测试.两个测试显示速度增加?10%,但两者之间的速度差异仍然相似.

# JMH 1.13 (released 45 days ago)

# VM version: JDK 9-ea, VM 9-ea+134

# VM invoker: /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/bin/java

# VM options:

Benchmark Mode Cnt Score Error Units

DirectByteBufferReadBenchmark.testReadAbsolute thrpt 15 102.170 ± 10.199 ops/s

DirectByteBufferReadBenchmark.testReadRelative thrpt 15 45.988 ± 3.896 ops/s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值