Java Array Sum sumFast(final double... values)

Here you can find the source of sumFast(final double... values)

Description

Numerically naive implementation of sum which is faster than MathUtils.sum() and sumNaive() Generally exhibits rounding error which grows with the length of the sum Note that it may not agree with other implementations due to optimizations which change the order of iteration which can affect the rounding error.

License

Open Source License

Parameter

Parameter Description
values a parameter

Return

Side Effects: none

Declaration

public static final double sumFast(final double... values) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w.j  a  v a2  s  .com*/
 * ====================================================
 * Copyright (C) 2013 by Idylwood Technologies, LLC. All rights reserved.
 *
 * Developed at Idylwood Technologies, LLC.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The License should have been distributed to you with the source tree.
 * If not, it can be found at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Charles Cooper
 * Date: 2013
 * ====================================================
 */

public class Main {
    /**
     * Numerically naive implementation of sum which is faster than MathUtils.sum() and sumNaive()
     * Generally exhibits rounding error which grows with the length of the sum
     * Note that it may not agree with other implementations
     * due to optimizations which change the order of iteration
     * which can affect the rounding error.
     * It is O(n) in the length of the array to be summed.
     * It is faster than the naive, unoptimized implementation by 20-40%
     * (dependent on the mood of the JIT) on my machine.
     * @param values
     * @return
     * Side Effects: none
     */
    public static final double sumFast(final double... values) {
        double ret = 0;
        // unroll the loop since the JIT shouldn't
        final int unroll = 4; // empirically unrolling more than 3 doesn't help much
        final int len = values.length - values.length % unroll;
        int i = 0;
        for (; i < len; i += unroll)
            ret += values[i] + values[i + 1] + values[i + 2] + values[i + 3];
        for (; i < values.length; i++)
            ret += values[i];
        return ret;
    }
}

Related

  1. sumDigits(final long value, final int... digits)
  2. sumDoubleArray(double[] double_array)
  3. sumDoubles(Double[] ds)
  4. sumEachFigureFactor(int[] numbers)
  5. sumElems(boolean[] inputRow)
  6. sumIntArray(int[] a)
  7. sumIntArray(int[] array)
  8. sumInts(int... numbers)
  9. sumList(int[] paramList)