Skip to content

Instantly share code, notes, and snippets.

@Aenigma
Created December 2, 2014 20:04
Show Gist options
  • Save Aenigma/8253677c27c44d08f058 to your computer and use it in GitHub Desktop.
Save Aenigma/8253677c27c44d08f058 to your computer and use it in GitHub Desktop.
/*
* Copyright 2014 Kevin Raoofi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License 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.
*/
package edu.frostburg.cosc310.skraoofi0.benchmarking;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Random;
public class Average {
static BigInteger bi = BigInteger.ZERO;
public static void main(String... args) {
Random r = new Random(0);
double avg = 0;
long count = 0;
long[] data = new long[1024];
for (int i = 0; i < data.length; i++) {
data[i] = Math.abs(r.nextLong());
}
for (int i = 0; i < data.length; i++) {
avg += ((double) data[i]) / (data.length);
}
System.out.println("Average 1: " + avg);
for (int i = 0; i < data.length; i++) {
count += data[i];
}
System.out.println("Average 2: " + count / data.length);
System.out.println("Average 3: " + Arrays.stream(data).average().getAsDouble());
Arrays.stream(data).forEach((l) -> {
bi = bi.add(new BigInteger(Long.toString(l)));
});
BigDecimal bd = new BigDecimal(bi);
bd = bd.divide(new BigDecimal(data.length), RoundingMode.UP);
System.out.println("Average 4: " + bd);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment