我正在玩Java 8,并使用程序比较Java 6中的一些东西来计算大型列表中偶数的总和
Java 8
public class ListOperationsJava8 {
static List<BigInteger> list = new LinkedList<>();
public static void main(String[] args) {
createList();
long start = System.currentTimeMillis();
/*System.out.println(list.parallelStream().
filter(n -> n.mod(new BigInteger("2")).equals(BigInteger.ZERO)).
mapToInt(BigInteger::intValue).sum()); --> gives result -1795017296 */
System.out.println(list.parallelStream().
filter(n -> n.mod(new BigInteger("2")).equals(BigInteger.ZERO)).
mapToLong(BigInteger::longValue).sum());
long end = System.currentTimeMillis();
System.out.println("Time taken using Java 8: " + (end - start) + " ms");
}
private static void createList() {
for (int i = 0; i < 100000; i++) {
list.add(new BigInteger(String.valueOf(i)));
}
}
}
Java 6
public class ListOperationsClassic {
static List<BigInteger> list = new LinkedList<BigInteger>();
public static void main(String[] args) {
createList();
long start = System.currentTimeMillis();
BigInteger sum = BigInteger.ZERO;
for(BigInteger n : list) {
if(n.mod(new BigInteger("2")).equals(BigInteger.ZERO))
sum = sum.add(n);
}
System.out.println(sum);
long end = System.currentTimeMillis();
System.out.println("Time taken using Java 6: " + (end - start) + " ms");
}
private static void createList() {
for (int i = 0; i < 100000; i++) {
list.add(new BigInteger(String.valueOf(i)));
}
}
}
我有两个问题
>在Java 8代码中,我最初使用过
mapToInt(BigInteger :: intValue).sum())减少了值,但是
得到了负面结果-1795017296!为什么?预期结果2499950000
本身在我可以用int表示的范围内
了解.
>我多次运行代码,我总是在Java 8中找到代码
比使用Java 6的代码花费的时间多5倍.那是什么
可能意味着?对于这种规模的操作,不值得使用
parallelStream和/或reduce操作和plain old for循环
更好?
以下是其中一个结果:
2499950000
Time taken using Java 6: 52 ms
2499950000
Time taken using Java 8: 249 ms
解决方法:
这是我快速而肮脏的基准测试,允许在每次测试之间进行JIT热身和GC测试.结果:
> for循环:686毫秒
> lamdbda:681毫秒
>平行lambda:405 ms
请注意,我已修改您的代码以使三个测试尽可能相等 – 特别是对于lambdas,我使用缩减来添加BigIntegers而不是转换为long.
我还在每次迭代中删除了不必要的新BigInteger(“2”)的创建.
public class Test1 {
static List<BigInteger> list = new LinkedList<>();
static BigInteger TWO = new BigInteger("2");
public static void main(String[] args) {
createList();
long sum = 0;
//warm-up
for (int i = 0; i < 100; i++) {
sum += forLoop().longValue();
sum += lambda().longValue();
sum += parallelLambda().longValue();
}
{
System.gc();
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) sum += forLoop().longValue();
long end = System.currentTimeMillis();
System.out.println("Time taken using for loop: " + (end - start) + " ms");
}
{
System.gc();
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) sum += lambda().longValue();
long end = System.currentTimeMillis();
System.out.println("Time taken using lambda: " + (end - start) + " ms");
}
{
System.gc();
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) sum += parallelLambda().longValue();
long end = System.currentTimeMillis();
System.out.println("Time taken using parallelLambda: " + (end - start) + " ms");
}
}
private static void createList() {
for (int i = 0; i < 100000; i++) {
list.add(new BigInteger(String.valueOf(i)));
}
}
private static BigInteger forLoop() {
BigInteger sum = BigInteger.ZERO;
for(BigInteger n : list) {
if(n.mod(TWO).equals(BigInteger.ZERO))
sum = sum.add(n);
}
return sum;
}
private static BigInteger lambda() {
return list.stream().
filter(n -> n.mod(TWO).equals(ZERO)).
reduce(ZERO, BigInteger::add);
}
private static BigInteger parallelLambda() {
return list.parallelStream().
filter(n -> n.mod(TWO).equals(ZERO)).
reduce(ZERO, BigInteger::add);
}
}