Skip to content

Commit 047486b

Browse files
committed
...
0 parents  commit 047486b

14 files changed

+735
-0
lines changed

.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin/*

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>hyperdrive</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Mon Aug 09 10:08:54 EDT 2010
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.6
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.6

src/org/api/hyperdrive/Coord.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.api.hyperdrive;
2+
3+
/**
4+
* Utility methods for dealing with coordinate arrays
5+
*/
6+
public class Coord
7+
{
8+
/**
9+
* Lexicographically increment a coordinate array
10+
*
11+
* @param coords Coordinate array
12+
* @param limits Limits of space (usually space's dimensions)
13+
* @return False if we've wrapped at the limits
14+
*/
15+
public static final boolean advance(int[] coords,int[] limits)
16+
{
17+
for(int i=0;i<coords.length;++i) {
18+
if (++coords[i] < limits[i])
19+
return true;
20+
else coords[i] = 0;
21+
}
22+
return false;
23+
}
24+
25+
/**
26+
* Get a comma-delimited representation of a coordinate array
27+
*
28+
* @param coords Coordinate array
29+
* @return Comma-delimited representation
30+
*/
31+
public static final String toString(int[] coords)
32+
{
33+
StringBuffer tmp = new StringBuffer(coords.length * 6);
34+
for(int i=0;i<coords.length;++i) {
35+
if (i > 0)
36+
tmp.append(',');
37+
tmp.append(Integer.toString(coords[i]));
38+
}
39+
return tmp.toString();
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
package org.api.hyperdrive;
2+
3+
import java.util.Random;
4+
5+
public class HyperdriveTester
6+
{
7+
public static void runTest()
8+
{
9+
System.out.println("Testing with a 3D array...");
10+
11+
Random prng = new Random(System.currentTimeMillis());
12+
int[][][] testReference = new int[991][997][7];
13+
int[] dimensions = new int[3];
14+
dimensions[0] = 991;
15+
dimensions[1] = 997;
16+
dimensions[2] = 7;
17+
int[] coords = new int[3];
18+
NArrayInt testArray = new NArrayInt(dimensions);
19+
System.out.println("Created array with dimensions ["+Coord.toString(dimensions)+"], size: "+testArray.size());
20+
coords[0] = 990;
21+
coords[1] = 996;
22+
coords[2] = 6;
23+
System.out.println("Index of max coords ["+Coord.toString(coords)+"]: "+testArray.indexOf(coords));
24+
for(int x=0;x<991;++x) {
25+
for(int y=0;y<997;++y) {
26+
for(int z=0;z<7;++z) {
27+
int foo = prng.nextInt();
28+
testReference[x][y][z] = foo;
29+
coords[0] = x;
30+
coords[1] = y;
31+
coords[2] = z;
32+
try {
33+
testArray.setInt(coords,foo);
34+
} catch (Exception e) {
35+
System.out.println("Failed setInt() at "+Coord.toString(coords));
36+
System.out.println("Size of array: "+testArray.size());
37+
e.printStackTrace();
38+
throw new RuntimeException("Test failed at "+Coord.toString(coords));
39+
}
40+
}
41+
}
42+
}
43+
for(int x=0;x<991;++x) {
44+
for(int y=0;y<997;++y) {
45+
for(int z=0;z<7;++z) {
46+
coords[0] = x;
47+
coords[1] = y;
48+
coords[2] = z;
49+
if (testArray.get(coords) != testReference[x][y][z])
50+
throw new RuntimeException("Test failed at "+Coord.toString(coords));
51+
}
52+
}
53+
}
54+
System.out.println("Passed.");
55+
}
56+
57+
public static void runBenchmarks()
58+
{
59+
System.out.println("Benchmarking native Java arrays...");
60+
61+
System.out.print(" 1 D: "); System.out.flush();
62+
int[] ja1d = new int[1048576];
63+
int[] coords = new int[1];
64+
int[] limits = new int[coords.length];
65+
limits[0] = ja1d.length;
66+
long lookups = 0;
67+
long startTime = System.currentTimeMillis();
68+
for(int k=0;k<128;++k) {
69+
for(int i=0;i<1048576;++i) {
70+
ja1d[coords[0]] = i + k;
71+
Coord.advance(coords,limits);
72+
++lookups;
73+
}
74+
}
75+
long endTime = System.currentTimeMillis();
76+
System.out.println(Double.toString(((double)lookups) / ((double)(endTime - startTime)))+" lookups/ms");
77+
ja1d = null;
78+
System.gc();
79+
80+
System.out.print(" 2 D: "); System.out.flush();
81+
int[][] ja2d = new int[1024][1024];
82+
coords = new int[2];
83+
limits = new int[coords.length];
84+
limits[0] = ja2d.length;
85+
limits[1] = ja2d[0].length;
86+
lookups = 0;
87+
startTime = System.currentTimeMillis();
88+
for(int k=0;k<128;++k) {
89+
for(int i=0;i<(1024*1024);++i) {
90+
ja2d[coords[0]][coords[1]] = i + k;
91+
Coord.advance(coords,limits);
92+
++lookups;
93+
}
94+
}
95+
endTime = System.currentTimeMillis();
96+
System.out.println(Double.toString(((double)lookups) / ((double)(endTime - startTime)))+" lookups/ms");
97+
ja2d = null;
98+
System.gc();
99+
100+
System.out.print(" 3 D: "); System.out.flush();
101+
int[][][] ja3d = new int[96][96][96];
102+
coords = new int[3];
103+
limits = new int[coords.length];
104+
limits[0] = ja3d.length;
105+
limits[1] = ja3d[0].length;
106+
limits[2] = ja3d[0][0].length;
107+
lookups = 0;
108+
startTime = System.currentTimeMillis();
109+
for(int k=0;k<128;++k) {
110+
for(int i=0;i<(96*96*96);++i) {
111+
ja3d[coords[0]][coords[1]][coords[2]] = i + k;
112+
Coord.advance(coords,limits);
113+
++lookups;
114+
}
115+
}
116+
endTime = System.currentTimeMillis();
117+
System.out.println(Double.toString(((double)lookups) / ((double)(endTime - startTime)))+" lookups/ms");
118+
ja3d = null;
119+
System.gc();
120+
121+
System.out.print(" 4 D: "); System.out.flush();
122+
int[][][][] ja4d = new int[32][32][32][32];
123+
coords = new int[4];
124+
limits = new int[coords.length];
125+
limits[0] = ja4d.length;
126+
limits[1] = ja4d[0].length;
127+
limits[2] = ja4d[0][0].length;
128+
limits[3] = ja4d[0][0][0].length;
129+
lookups = 0;
130+
startTime = System.currentTimeMillis();
131+
for(int k=0;k<128;++k) {
132+
for(int i=0;i<(32*32*32*32);++i) {
133+
ja4d[coords[0]][coords[1]][coords[2]][coords[3]] = i + k;
134+
Coord.advance(coords,limits);
135+
++lookups;
136+
}
137+
}
138+
endTime = System.currentTimeMillis();
139+
System.out.println(Double.toString(((double)lookups) / ((double)(endTime - startTime)))+" lookups/ms");
140+
ja4d = null;
141+
System.gc();
142+
143+
System.out.print(" 5 D: "); System.out.flush();
144+
int[][][][][] ja5d = new int[16][16][16][16][16];
145+
coords = new int[5];
146+
limits = new int[coords.length];
147+
limits[0] = ja5d.length;
148+
limits[1] = ja5d[0].length;
149+
limits[2] = ja5d[0][0].length;
150+
limits[3] = ja5d[0][0][0].length;
151+
limits[4] = ja5d[0][0][0][0].length;
152+
lookups = 0;
153+
startTime = System.currentTimeMillis();
154+
for(int k=0;k<128;++k) {
155+
for(int i=0;i<(16*16*16*16*16);++i) {
156+
ja5d[coords[0]][coords[1]][coords[2]][coords[3]][coords[4]] = i + k;
157+
Coord.advance(coords,limits);
158+
++lookups;
159+
}
160+
}
161+
endTime = System.currentTimeMillis();
162+
System.out.println(Double.toString(((double)lookups) / ((double)(endTime - startTime)))+" lookups/ms");
163+
ja5d = null;
164+
System.gc();
165+
166+
System.out.println();
167+
168+
System.out.println("Benchmarking integer NArrays of similar size...");
169+
for(int n=1;n<=5;++n) {
170+
System.out.print(" "+n+" D: "); System.out.flush();
171+
coords = new int[n];
172+
limits = new int[n];
173+
switch(n) {
174+
case 1:
175+
limits[0] = 1048576;
176+
break;
177+
case 2:
178+
limits[0] = 1024;
179+
limits[1] = 1024;
180+
break;
181+
case 3:
182+
limits[0] = 96;
183+
limits[1] = 96;
184+
limits[2] = 96;
185+
break;
186+
case 4:
187+
limits[0] = 32;
188+
limits[1] = 32;
189+
limits[2] = 32;
190+
limits[3] = 32;
191+
break;
192+
case 5:
193+
limits[0] = 16;
194+
limits[1] = 16;
195+
limits[2] = 16;
196+
limits[3] = 16;
197+
limits[4] = 16;
198+
break;
199+
}
200+
NArrayInt a = new NArrayInt(limits);
201+
lookups = 0;
202+
startTime = System.currentTimeMillis();
203+
for(int k=0;k<128;++k) {
204+
for(int i=0,j=a.size();i<j;++i) {
205+
a.setInt(coords,i + k);
206+
Coord.advance(coords,limits);
207+
++lookups;
208+
}
209+
}
210+
endTime = System.currentTimeMillis();
211+
System.out.println(Double.toString(((double)lookups) / ((double)(endTime - startTime)))+" lookups/ms");
212+
}
213+
}
214+
215+
public static void main(String[] argv)
216+
{
217+
runTest();
218+
System.gc();
219+
System.out.println();
220+
try {
221+
Thread.sleep(1000L);
222+
} catch (Exception e) {}
223+
runBenchmarks();
224+
}
225+
}

0 commit comments

Comments
 (0)