private void initValues(final long[] lengths, final StructuredArray<MockStructure> array) { final long[] cursors = new long[lengths.length]; final long totalElementCount = array.getTotalElementCount(); long elementCountToCursor = 0; while (elementCountToCursor < totalElementCount) { // Check element at cursors: MockStructure mockStructure = array.get(cursors); long indexSum = 0; for (long index : cursors) { indexSum += index; } mockStructure.setIndex(indexSum); mockStructure.setTestValue(indexSum * 2); // Increment cursors from inner-most dimension out: for (int cursorDimension = cursors.length - 1; cursorDimension >= 0; cursorDimension--) { if ((++cursors[cursorDimension]) < lengths[cursorDimension]) break; // This dimension wrapped. Reset to zero and continue to one dimension higher cursors[cursorDimension] = 0; } elementCountToCursor++; } }
long loopSumTest() { long sum = 0; for (int i = 0; i < array.getLength(); i++) { sum += array.get(i).getTestValue(); } return sum; }
private void assertCorrectFixedInitialisation( final long expectedIndex, final long expectedValue, final long[] lengths, final StructuredArray<MockStructure> array) { for (int i = 0; i < lengths.length; i++) { assertThat(valueOf(array.getLengths()[i]), is(valueOf(lengths[i]))); } assertTrue(array.getElementClass() == MockStructure.class); final long[] cursors = new long[lengths.length]; final long totalElementCount = array.getTotalElementCount(); long elementCountToCursor = 0; while (elementCountToCursor < totalElementCount) { // Check element at cursors: MockStructure mockStructure = array.get(cursors); assertThat(valueOf(mockStructure.getIndex()), is(valueOf(expectedIndex))); assertThat(valueOf(mockStructure.getTestValue()), is(valueOf(expectedValue))); // Increment cursors from inner-most dimension out: for (int cursorDimension = cursors.length - 1; cursorDimension >= 0; cursorDimension--) { if ((++cursors[cursorDimension]) < lengths[cursorDimension]) break; // This dimension wrapped. Reset to zero and continue to one dimension higher cursors[cursorDimension] = 0; } elementCountToCursor++; } }
@Test public void testLoopingSpeeds() throws NoSuchMethodException { final int length = 10000000; final CtorAndArgsProvider<MockStructure> ctorAndArgsProvider = new DefaultMockCtorAndArgsProvider(); array = StructuredArray.newInstance(ctorAndArgsProvider, length); encapsulatedArray = new EncapsulatedArray(length); for (int i = 0; i < 5; i++) { long startTime1 = System.nanoTime(); long sum1 = loopSumTest(); long endTime1 = System.nanoTime(); double loopsPerSec1 = 1000 * (double) length / (endTime1 - startTime1); long startTime2 = System.nanoTime(); long sum2 = loopEncapsulatedArraySumTest(); long endTime2 = System.nanoTime(); double loopsPerSec2 = 1000 * (double) length / (endTime2 - startTime2); System.out.println( "sum1 = " + sum1 + " (" + loopsPerSec1 + "M), sum2 = " + sum2 + " (" + loopsPerSec2 + "M)"); } }