Skip to content

Instantly share code, notes, and snippets.

@Nessiesson
Last active October 26, 2023 18:09
Show Gist options
  • Save Nessiesson/faec0ba9c064c47bf107ec98c00bbd49 to your computer and use it in GitHub Desktop.
Save Nessiesson/faec0ba9c064c47bf107ec98c00bbd49 to your computer and use it in GitHub Desktop.
oh no
package net.dugged.nessie;
import net.jafama.FastMath;
import org.lwjgl.BufferUtils;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import java.nio.FloatBuffer;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
public class JHMTestertoo {
private final FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
@Param({"1000000"})
public int iterations;
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void array(Blackhole bh) {
for (int i = 0; i < this.iterations; i++) {
var rotateAngleX = Math.random() * 720F - 360F;
var rotateAngleY = Math.random() * 720F - 360F;
var rotateAngleZ = Math.random() * 720F - 360F;
final float cosX = (float) FastMath.cosQuick(rotateAngleX);
final float sinX = (float) FastMath.sinQuick(rotateAngleX);
final float cosY = (float) FastMath.cosQuick(rotateAngleY);
final float sinY = (float) FastMath.sinQuick(rotateAngleY);
final float cosZ = (float) FastMath.cosQuick(rotateAngleZ);
final float sinZ = (float) FastMath.sinQuick(rotateAngleZ);
final float[] matrix = {
cosY * cosZ, cosY * sinZ, -sinY, 0,
sinX * sinY * cosZ - cosX * sinZ, cosX * cosZ + sinX * sinY * sinZ, sinX * cosY, 0,
cosX * sinY * cosZ + sinX * sinZ, -cosZ * sinX + cosX * sinY * sinZ, cosX * cosY, 0,
0, 0, 0, 1
};
this.buffer.put(matrix).flip();
bh.consume(matrix);
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void buffer(Blackhole bh) {
this.buffer.limit(16);
this.buffer.put(3, 0);
this.buffer.put(7, 0);
this.buffer.put(11, 0);
this.buffer.put(12, 0);
this.buffer.put(13, 0);
this.buffer.put(14, 0);
this.buffer.put(15, 1);
for (int i = 0; i < this.iterations; i++) {
this.buffer.limit(16);
var rotateAngleX = Math.random() * 720F - 360F;
var rotateAngleY = Math.random() * 720F - 360F;
var rotateAngleZ = Math.random() * 720F - 360F;
final float cosX = (float) FastMath.cosQuick(rotateAngleX);
final float sinX = (float) FastMath.sinQuick(rotateAngleX);
final float cosY = (float) FastMath.cosQuick(rotateAngleY);
final float sinY = (float) FastMath.sinQuick(rotateAngleY);
final float cosZ = (float) FastMath.cosQuick(rotateAngleZ);
final float sinZ = (float) FastMath.sinQuick(rotateAngleZ);
this.buffer.put(0, cosY * cosZ);
this.buffer.put(1, cosY * sinZ);
this.buffer.put(2, -sinY);
this.buffer.put(4, sinX * sinY * cosZ - cosX * sinZ);
this.buffer.put(5, cosX * cosZ + sinX * sinY * sinZ);
this.buffer.put(6, sinX * cosY);
this.buffer.put(8, cosX * sinY * cosZ + sinX * sinZ);
this.buffer.put(9, -cosZ * sinX + cosX * sinY * sinZ);
this.buffer.put(10, cosX * cosY);
this.buffer.flip();
bh.consume(this.buffer);
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void bufferChained(Blackhole bh) {
this.buffer.limit(16)
.put(3, 0)
.put(7, 0)
.put(11, 0)
.put(12, 0)
.put(13, 0)
.put(14, 0)
.put(15, 1);
for (int i = 0; i < this.iterations; i++) {
var rotateAngleX = Math.random() * 720F - 360F;
var rotateAngleY = Math.random() * 720F - 360F;
var rotateAngleZ = Math.random() * 720F - 360F;
final float cosX = (float) FastMath.cosQuick(rotateAngleX);
final float sinX = (float) FastMath.sinQuick(rotateAngleX);
final float cosY = (float) FastMath.cosQuick(rotateAngleY);
final float sinY = (float) FastMath.sinQuick(rotateAngleY);
final float cosZ = (float) FastMath.cosQuick(rotateAngleZ);
final float sinZ = (float) FastMath.sinQuick(rotateAngleZ);
this.buffer.limit(16)
.put(0, cosY * cosZ)
.put(1, cosY * sinZ)
.put(2, -sinY)
.put(4, sinX * sinY * cosZ - cosX * sinZ)
.put(5, cosX * cosZ + sinX * sinY * sinZ)
.put(6, sinX * cosY)
.put(8, cosX * sinY * cosZ + sinX * sinZ)
.put(9, -cosZ * sinX + cosX * sinY * sinZ)
.put(10, cosX * cosY)
.flip();
bh.consume(this.buffer);
}
}
}
// Benchmark (iterations) Mode Cnt Score Error Units
// JHMTestertoo.array 1000000 avgt 5 31.890 ± 0.216 ms/op
// JHMTestertoo.buffer 1000000 avgt 5 19.279 ± 0.114 ms/op
// JHMTestertoo.bufferChained 1000000 avgt 5 18.926 ± 0.077 ms/op
// Warmup: 5 iterations, 20 s each
// Measurement: 5 iterations, 20 s each
// Timeout: 10 min per iteration
// Threads: 1 thread, will synchronize iterations
// Benchmark mode: Average time, time/op
// Parameters: (iterations = 1000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment