Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 50fb61d

Browse files
committedJun 6, 2020
use System.arraycopy instead of loop for better performance.
1 parent 715d89d commit 50fb61d

File tree

9 files changed

+21
-39
lines changed

9 files changed

+21
-39
lines changed
 

‎src/main/java/org/apache/commons/math4/analysis/solvers/LaguerreSolver.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,7 @@ public Complex[] solveAll(Complex coefficients[], Complex initial)
275275
}
276276
// Coefficients for deflated polynomial.
277277
final Complex c[] = new Complex[n + 1];
278-
for (int i = 0; i <= n; i++) {
279-
c[i] = coefficients[i];
280-
}
278+
System.arraycopy(coefficients, 0, c, 0, n + 1);
281279

282280
// Solve individual roots successively.
283281
final Complex root[] = new Complex[n];

‎src/main/java/org/apache/commons/math4/dfp/Dfp.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,8 @@ public Dfp getTwo() {
668668
/** Shift the mantissa left, and adjust the exponent to compensate.
669669
*/
670670
protected void shiftLeft() {
671-
for (int i = mant.length - 1; i > 0; i--) {
672-
mant[i] = mant[i-1];
671+
if (mant.length - 1 >= 0) {
672+
System.arraycopy(mant, 0, mant, 1, mant.length - 1);
673673
}
674674
mant[0] = 0;
675675
exp--;
@@ -680,8 +680,8 @@ uses shiftRight() */
680680
/** Shift the mantissa right, and adjust the exponent to compensate.
681681
*/
682682
protected void shiftRight() {
683-
for (int i = 0; i < mant.length - 1; i++) {
684-
mant[i] = mant[i+1];
683+
if (mant.length - 1 >= 0) {
684+
System.arraycopy(mant, 1, mant, 0, mant.length - 1);
685685
}
686686
mant[mant.length - 1] = 0;
687687
exp++;
@@ -1863,9 +1863,7 @@ public Dfp divide(Dfp divisor) {
18631863

18641864
/* move the remainder into the dividend while left shifting */
18651865
dividend[0] = 0;
1866-
for (int i = 0; i < mant.length; i++) {
1867-
dividend[i + 1] = remainder[i];
1868-
}
1866+
System.arraycopy(remainder, 0, dividend, 1, mant.length);
18691867
}
18701868

18711869
/* Find the most sig digit */

‎src/main/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizer.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,8 @@ public Optimum optimize(final LeastSquaresProblem problem) {
348348

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

355354
// compute Qt.res
356355
qTy(qtf, internalData);

‎src/main/java/org/apache/commons/math4/linear/HessenbergTransformer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ public RealMatrix getH() {
150150
}
151151

152152
// copy upper triangular part of the matrix
153-
for (int j = i; j < m; ++j) {
154-
h[i][j] = householderVectors[i][j];
155-
}
153+
System.arraycopy(householderVectors[i], i, h[i], i, m - i);
156154
}
157155
cachedH = MatrixUtils.createRealMatrix(h);
158156
}

‎src/main/java/org/apache/commons/math4/ml/neuralnet/twod/NeuronSquareMesh2D.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,8 @@ private NeuronSquareMesh2D(boolean wrapRowDim,
237237
public synchronized NeuronSquareMesh2D copy() {
238238
final long[][] idGrid = new long[numberOfRows][numberOfColumns];
239239
for (int r = 0; r < numberOfRows; r++) {
240-
for (int c = 0; c < numberOfColumns; c++) {
241-
idGrid[r][c] = identifiers[r][c];
242-
}
240+
if (numberOfColumns >= 0)
241+
System.arraycopy(identifiers[r], 0, idGrid[r], 0, numberOfColumns);
243242
}
244243

245244
return new NeuronSquareMesh2D(wrapRows,

‎src/main/java/org/apache/commons/math4/ode/nonstiff/AdamsNordsieckFieldTransformer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ private AdamsNordsieckFieldTransformer(final Field<T> field, final int n) {
170170
// Nordsieck to multistep, then shifting rows to represent step advance
171171
// then applying inverse transform
172172
T[][] shiftedP = bigP.getData();
173-
for (int i = shiftedP.length - 1; i > 0; --i) {
174-
// shift rows
175-
shiftedP[i] = shiftedP[i - 1];
173+
// shift rows
174+
if (shiftedP.length - 1 >= 0) {
175+
System.arraycopy(shiftedP, 0, shiftedP, 1, shiftedP.length - 1);
176176
}
177177
shiftedP[0] = MathArrays.buildArray(field, rows);
178178
Arrays.fill(shiftedP[0], field.getZero());

‎src/main/java/org/apache/commons/math4/optim/nonlinear/scalar/noderiv/CMAESOptimizer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,8 @@ private void updateBD(double negccov) {
811811
* @param val Current best fitness value.
812812
*/
813813
private static void push(double[] vals, double val) {
814-
for (int i = vals.length-1; i > 0; i--) {
815-
vals[i] = vals[i-1];
814+
if (vals.length - 1 >= 0) {
815+
System.arraycopy(vals, 0, vals, 1, vals.length - 1);
816816
}
817817
vals[0] = val;
818818
}

‎src/test/java/org/apache/commons/math4/linear/RealVectorAbstractTest.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,7 @@ public void testSetSubVectorSameType() {
406406
final int index = 2;
407407
actual.setSubVector(index, create(sub));
408408

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

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

424-
for (int i = 0; i < sub.length; i++){
425-
expected[index + i] = sub[i];
426-
}
422+
System.arraycopy(sub, 0, expected, 2, sub.length);
427423
TestUtils.assertEquals("", expected, actual, 0d);
428424
}
429425

‎src/test/java/org/apache/commons/math4/optim/nonlinear/scalar/noderiv/PowellOptimizerTest.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ public void testSumSinc() {
6161
double[] init = new double[dim];
6262

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

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

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

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

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

136130
// Initial is far from minimum.

0 commit comments

Comments
 (0)
Please sign in to comment.