Skip to content

Instantly share code, notes, and snippets.

@gurinderu
Last active April 29, 2017 11:05
Show Gist options
  • Save gurinderu/93f42cc403f30d0cc93803a5a995f7e4 to your computer and use it in GitHub Desktop.
Save gurinderu/93f42cc403f30d0cc93803a5a995f7e4 to your computer and use it in GitHub Desktop.
package lol;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
/**
* Created by grinder on 29.04.17.
*/
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(value = 3, jvmArgsAppend = {"-XX:+UseParallelGC", "-Xms1g", "-Xmx1g"})
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class HeapVsByteBuffer {
private ByteBuffer buffer;
private ByteBuffer directBuffer;
private byte[] array;
private byte value = 0b01;
@Setup
public void setup() {
array = new byte[10];
buffer = ByteBuffer.allocate(10);
directBuffer = ByteBuffer.allocateDirect(10);
}
@Group("write")
@Benchmark
public void heapWrite() {
array[5]=value;
}
@Group("write")
@Benchmark
public void byteBufferWrite() {
buffer.put(5, value);
}
@Group("write")
@Benchmark
public void byteBufferDirectWrite() {
directBuffer.put(5, value);
}
@Group("get")
@Benchmark
public int heapGet() {
return array[5];
}
@Group("get")
@Benchmark
public int byteBufferGet() {
return buffer.get(5);
}
@Group("get")
@Benchmark
public int byteBufferDirectGet() {
return directBuffer.get(5);
}
}
@gurinderu
Copy link
Author

gurinderu commented Apr 29, 2017

Benchmark Mode Cnt Score Error Units
HeapVsByteBuffer.get thrpt 15 0,833 ± 0,041 ops/ns
HeapVsByteBuffer.get:byteBufferDirectGet thrpt 15 0,279 ± 0,014 ops/ns
HeapVsByteBuffer.get:byteBufferGet thrpt 15 0,235 ± 0,012 ops/ns
HeapVsByteBuffer.get:heapGet thrpt 15 0,319 ± 0,015 ops/ns
HeapVsByteBuffer.write thrpt 15 1,414 ± 0,202 ops/ns
HeapVsByteBuffer.write:byteBufferDirectWrite thrpt 15 0,087 ± 0,016 ops/ns
HeapVsByteBuffer.write:byteBufferWrite thrpt 15 0,368 ± 0,176 ops/ns
HeapVsByteBuffer.write:heapWrite thrpt 15 0,958 ± 0,106 ops/ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment