Skip to content

Instantly share code, notes, and snippets.

@kabutz
Created November 28, 2021 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kabutz/ee2544e726fb1aa9c1f89c873e57960d to your computer and use it in GitHub Desktop.
Save kabutz/ee2544e726fb1aa9c1f89c873e57960d to your computer and use it in GitHub Desktop.
package richardstartin;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
public class CompositePixelLookup {
record Pixel(int x, int y) {
}
Map<String, Object> concatMap;
Map<Pixel, Object> pixelMap;
@Param("1024")
int width;
@Param("768")
int height;
@Setup(Level.Trial)
public void setup() {
concatMap = new HashMap<>();
pixelMap = new HashMap<>();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
concatMap.put(x + ";" + y, x * 768 + y);
pixelMap.put(new Pixel(x, y), x * 768 + y);
}
}
}
@Benchmark
@OperationsPerInvocation(786432)
public void concatenate(Blackhole bh) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bh.consume(concatMap.get(x + ";" + y));
}
}
}
@Benchmark
@OperationsPerInvocation(786432)
public void wrap(Blackhole bh) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bh.consume(pixelMap.get(new Pixel(x, y)));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment