Skip to content

[MATH-1539] use System.arraycopy instead of loop for better performance. #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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];
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
@@ -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) {
System.arraycopy(mant, 0, mant, 1, mant.length - 1);
}
mant[0] = 0;
exp--;
@@ -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) {
System.arraycopy(mant, 1, mant, 0, mant.length - 1);
}
mant[mant.length - 1] = 0;
exp++;
@@ -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 */
Original file line number Diff line number Diff line change
@@ -348,10 +348,9 @@ 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);

Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -237,8 +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);
}
}

Original file line number Diff line number Diff line change
@@ -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) {
System.arraycopy(shiftedP, 0, shiftedP, 1, shiftedP.length - 1);
}
shiftedP[0] = MathArrays.buildArray(field, rows);
Arrays.fill(shiftedP[0], field.getZero());
Original file line number Diff line number Diff line change
@@ -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) {
System.arraycopy(vals, 0, vals, 1, vals.length - 1);
}
vals[0] = val;
}
Original file line number Diff line number Diff line change
@@ -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);
}

@@ -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);
}

Original file line number Diff line number Diff line change
@@ -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.
@@ -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.
@@ -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.