second commit

This commit is contained in:
Shautvast 2023-08-12 09:18:37 +02:00
parent cba604354e
commit a8103b9c2e
7 changed files with 368 additions and 0 deletions

35
.gitignore vendored Normal file
View file

@ -0,0 +1,35 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

83
pom.xml Normal file
View file

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>MultiDim</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.36</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.36</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerVersion>9</compilerVersion>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>benchmark</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,30 @@
package com.github.shautvast.multidim;
/**
* Any dimensional int array, stored as an int[]
* No runtime checks! Easy to shoot yourself in the foot!
*/
public class Int2dArray {
private final int[] data;
private int cols;
public Int2dArray(int rows, int cols) {
data = new int[rows * cols];
this.cols = cols;
}
public int get(int row, int col) {
return row * cols + col;
}
public void set(int row, int col, int val) {
data[row * cols + col] = val;
}
int internalSize() {
return data.length;
}
}

View file

@ -0,0 +1,62 @@
package com.github.shautvast.multidim;
/**
* Any dimensional int array, stored as an int[]
* No runtime checks! Easy to shoot yourself in the foot!
*/
public class IntArray {
private final int[] data;
private final int[] dims;
public IntArray(int... dims) {
if (dims.length < 1) {
throw new IllegalArgumentException("array cannot have " + dims.length + " dimensions");
}
this.dims = dims;
int size = 1;
for (int dim : dims) {
size *= dim;
}
data = new int[size];
}
public int get(int... coords) {
if (coords.length < 1) {
throw new IllegalArgumentException("must supply at least one coordinate");
}
int index = 0;
if (coords.length > 1) {
for (int i = coords.length - 2; i >= 0; i--) {
index += coords[i] * dims[i + 1];
}
}
return data[index + coords[coords.length - 1]];
}
/**
* 'smart' use of varargs int: first values are the coordinates, last is the value itself
*
* @param coordsVal, all coordinates and the value to set
*/
public void set(int... coordsVal) {
if (coordsVal.length < 2) {
throw new IllegalArgumentException("must supply at least one coordinate and a value to set");
}
int index = 0;
if (coordsVal.length > 2) {
for (int i = coordsVal.length - 3; i >= 0; i--) {
index += coordsVal[i] * dims[i + 1];
}
}
data[index + coordsVal[coordsVal.length - 2]] = coordsVal[coordsVal.length - 1];
}
int internalSize() {
return data.length;
}
}

View file

@ -0,0 +1,130 @@
package com.github.shautvast.multidim;
import org.openjdk.jmh.annotations.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(value = 1)
public class JmhBenchmark {
private static final int ROWS = 100;
private static final int COLS = 100;
@org.openjdk.jmh.annotations.State(Scope.Thread)
public static class State {
Int2dArray seqInt2DArray;
IntArray intArray;
int[][] ints;
@Setup(Level.Iteration)
public void doSetup() {
long seed = System.currentTimeMillis();
Random random = new Random(seed);
ints = new int[COLS][ROWS];
intArray = new IntArray(COLS, ROWS);
seqInt2DArray = new Int2dArray(COLS, ROWS);
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
int i = random.nextInt();
intArray.set(r, c, i);
seqInt2DArray.set(r, c, i);
}
}
}
}
//@Benchmark
public Int2dArray seq2DArraySetTDLR(State state) {
Int2dArray d2Array = new Int2dArray(ROWS, COLS);
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
d2Array.set(r, c, r * c);
}
}
return d2Array;
}
//@Benchmark
public int[][] classicArraySet(State state) {
int[][] d2Array = new int[ROWS][COLS];
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
d2Array[r][c] = r * c;
}
}
return d2Array;
}
@Benchmark
public int classicArrayGetTDLR(State state) {
int t = 0;
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
t += state.ints[r][c];
}
}
return t;
}
@Benchmark
public int classicArrayGetLRTD(State state) {
int t = 0;
for (int c = 0; c < COLS; c++) {
for (int r = 0; r < ROWS; r++) {
t += state.ints[r][c];
}
}
return t;
}
// @Benchmark
public int seq2DArrayGetTDLR(State state) {
int t = 0;
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
t += state.seqInt2DArray.get(r, c);
}
}
return t;
}
//@Benchmark
public int seq2DArrayGetLRTD(State state) {
int t = 0;
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
t += state.seqInt2DArray.get(r, c);
}
}
return t;
}
//@Benchmark
public int seqMultArrayGetLRTD(State state) {
int t = 0xFFFFFFFF;
for (int c = 0; c < COLS; c++) {
for (int r = 0; r < ROWS; r++) {
t = state.intArray.get(r, c);
}
}
return t;
}
//@Benchmark
public int seqMultArrayGetTDLR(State state) {
int t = 0;
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
t = state.intArray.get(r, c);
}
}
return t;
}
}

View file

@ -0,0 +1,28 @@
package com.github.shautvast.multidim;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class IntArrayTest {
@Test
void test() {
IntArray intArray = new IntArray(2, 3, 5);
assertEquals(30, intArray.internalSize());
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 5; k++) {
intArray.set(i, j, k, 2 * 3 * i + 3 * j + k);
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 5; k++) {
assertEquals(2 * 3 * i, 3 * j + k, intArray.get(i, j, k));
}
}
}
}
}