Skip to content

Commit

Permalink
use System.arraycopy instead of loop for better performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
XenoAmess committed Jun 6, 2020
1 parent 715d89d commit 50fb61d
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,7 @@ public Complex[] solveAll(Complex coefficients[], Complex initial)
}
// Coefficients for deflated polynomial.
final Complex c[] = new Complex[n + 1];
for (int i = 0; i <= n; i++) {
c[i] = coefficients[i];
}
System.arraycopy(coefficients, 0, c, 0, n + 1);

// Solve individual roots successively.
final Complex root[] = new Complex[n];
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/org/apache/commons/math4/dfp/Dfp.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,8 @@ public Dfp getTwo() {
/** Shift the mantissa left, and adjust the exponent to compensate.
*/
protected void shiftLeft() {
for (int i = mant.length - 1; i > 0; i--) {
mant[i] = mant[i-1];
if (mant.length - 1 >= 0) {
System.arraycopy(mant, 0, mant, 1, mant.length - 1);
}
mant[0] = 0;
exp--;
Expand All @@ -680,8 +680,8 @@ uses shiftRight() */
/** Shift the mantissa right, and adjust the exponent to compensate.
*/
protected void shiftRight() {
for (int i = 0; i < mant.length - 1; i++) {
mant[i] = mant[i+1];
if (mant.length - 1 >= 0) {
System.arraycopy(mant, 1, mant, 0, mant.length - 1);
}
mant[mant.length - 1] = 0;
exp++;
Expand Down Expand Up @@ -1863,9 +1863,7 @@ public Dfp divide(Dfp divisor) {

/* move the remainder into the dividend while left shifting */
dividend[0] = 0;
for (int i = 0; i < mant.length; i++) {
dividend[i + 1] = remainder[i];
}
System.arraycopy(remainder, 0, dividend, 1, mant.length);
}

/* Find the most sig digit */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,8 @@ public Optimum optimize(final LeastSquaresProblem problem) {

//residuals already have weights applied
double[] weightedResidual = currentResiduals;
for (int i = 0; i < nR; i++) {
qtf[i] = weightedResidual[i];
}
if (nR >= 0)
System.arraycopy(weightedResidual, 0, qtf, 0, nR);

// compute Qt.res
qTy(qtf, internalData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ public RealMatrix getH() {
}

// copy upper triangular part of the matrix
for (int j = i; j < m; ++j) {
h[i][j] = householderVectors[i][j];
}
System.arraycopy(householderVectors[i], i, h[i], i, m - i);
}
cachedH = MatrixUtils.createRealMatrix(h);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,8 @@ private NeuronSquareMesh2D(boolean wrapRowDim,
public synchronized NeuronSquareMesh2D copy() {
final long[][] idGrid = new long[numberOfRows][numberOfColumns];
for (int r = 0; r < numberOfRows; r++) {
for (int c = 0; c < numberOfColumns; c++) {
idGrid[r][c] = identifiers[r][c];
}
if (numberOfColumns >= 0)
System.arraycopy(identifiers[r], 0, idGrid[r], 0, numberOfColumns);
}

return new NeuronSquareMesh2D(wrapRows,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ private AdamsNordsieckFieldTransformer(final Field<T> field, final int n) {
// Nordsieck to multistep, then shifting rows to represent step advance
// then applying inverse transform
T[][] shiftedP = bigP.getData();
for (int i = shiftedP.length - 1; i > 0; --i) {
// shift rows
shiftedP[i] = shiftedP[i - 1];
// shift rows
if (shiftedP.length - 1 >= 0) {
System.arraycopy(shiftedP, 0, shiftedP, 1, shiftedP.length - 1);
}
shiftedP[0] = MathArrays.buildArray(field, rows);
Arrays.fill(shiftedP[0], field.getZero());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,8 @@ private void updateBD(double negccov) {
* @param val Current best fitness value.
*/
private static void push(double[] vals, double val) {
for (int i = vals.length-1; i > 0; i--) {
vals[i] = vals[i-1];
if (vals.length - 1 >= 0) {
System.arraycopy(vals, 0, vals, 1, vals.length - 1);
}
vals[0] = val;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,7 @@ public void testSetSubVectorSameType() {
final int index = 2;
actual.setSubVector(index, create(sub));

for (int i = 0; i < sub.length; i++){
expected[index + i] = sub[i];
}
System.arraycopy(sub, 0, expected, 2, sub.length);
TestUtils.assertEquals("", expected, actual, 0d);
}

Expand All @@ -421,9 +419,7 @@ public void testSetSubVectorMixedType() {
final int index = 2;
actual.setSubVector(index, createAlien(sub));

for (int i = 0; i < sub.length; i++){
expected[index + i] = sub[i];
}
System.arraycopy(sub, 0, expected, 2, sub.length);
TestUtils.assertEquals("", expected, actual, 0d);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ public void testSumSinc() {
double[] init = new double[dim];

// Initial is minimum.
for (int i = 0; i < dim; i++) {
init[i] = minPoint[i];
}
System.arraycopy(minPoint, 0, init, 0, dim);
doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9);

// Initial is far from minimum.
Expand Down Expand Up @@ -96,9 +94,7 @@ public double value(double[] x) {
double[] init = new double[dim];

// Initial is minimum.
for (int i = 0; i < dim; i++) {
init[i] = minPoint[i];
}
System.arraycopy(minPoint, 0, init, 0, dim);
doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8);

// Initial is far from minimum.
Expand Down Expand Up @@ -128,9 +124,7 @@ public double value(double[] x) {
double[] init = new double[dim];

// Initial is minimum.
for (int i = 0; i < dim; i++) {
init[i] = maxPoint[i];
}
System.arraycopy(maxPoint, 0, init, 0, dim);
doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-9, 1e-8);

// Initial is far from minimum.
Expand Down

0 comments on commit 50fb61d

Please sign in to comment.