Skip to content

Instantly share code, notes, and snippets.

@stevenschlansker
Created October 24, 2013 19:37
Show Gist options
  • Save stevenschlansker/7143614 to your computer and use it in GitHub Desktop.
Save stevenschlansker/7143614 to your computer and use it in GitHub Desktop.
Benchmark Mode Thr Cnt Sec Mean Mean error Units
n.r.b.FloatBufferBenchmarks.testBufferAccess thrpt 1 16 1 97.628 0.034 ops/ms
n.r.b.FloatBufferBenchmarks.testDirectAccess thrpt 1 16 1 97.585 0.018 ops/ms
n.r.b.FloatBufferBenchmarks.testHeapAccess thrpt 1 16 1 97.732 0.009 ops/ms
n.r.b.FloatBufferBenchmarks.testSliceAccess thrpt 1 16 1 55.028 0.601 ops/ms
n.r.b.FloatBufferBenchmarks.testSliceNoCheckAcc thrpt 1 16 1 72.770 1.333 ops/ms
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Random;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.State;
import io.airlift.slice.Slice;
public class FloatBufferBenchmarks
{
private static final int SIZE = 100;
@State
public static class Buffers
{
float[][] heap = new float[SIZE][SIZE];
FloatBuffer buffer = FloatBuffer.allocate(SIZE * SIZE);
FloatBuffer direct = ByteBuffer.allocateDirect(SIZE * SIZE * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
Slice slice = Slice.toUnsafeSlice(ByteBuffer.allocateDirect(SIZE * SIZE * 4).order(ByteOrder.nativeOrder()));
public Buffers()
{
Random r = new Random();
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
final int index = i * SIZE + j;
float f = r.nextFloat();
heap[i][j] = f;
buffer.put(index, f);
direct.put(index, f);
slice.setFloat(index * 4, f);
}
}
}
}
@GenerateMicroBenchmark
public float testHeapAccess(Buffers b)
{
float accum = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
accum += b.heap[i][j];
}
}
return accum;
}
@GenerateMicroBenchmark
public float testBufferAccess(Buffers b)
{
float accum = 0;
for (int i = 0; i < SIZE * SIZE; i++) {
accum += b.buffer.get(i);
}
return accum;
}
@GenerateMicroBenchmark
public float testDirectAccess(Buffers b)
{
float accum = 0;
for (int i = 0; i < SIZE * SIZE; i++) {
accum += b.direct.get(i);
}
return accum;
}
@GenerateMicroBenchmark
public float testSliceAccess(Buffers b)
{
float accum = 0;
for (int i = 0; i < SIZE * SIZE * 4; i+= 4) {
accum += b.slice.getFloat(i);
}
return accum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment