0

我正在阅读“Java 8 In Action”(Raoul-Gabriel Urma、Mario Fusco 和 Alan Mycroft 着),第 5.6.3 节,第 116 和 117 页。提供的代码处理所谓的“毕达哥拉斯三元组”的计算。第 116 页显示了第一次尝试,第 117 页显示了生成这些三元组的改进尝试,两者都使用“.rangeClosed()”方法。

我发现了一些超出书本的优化,我想在这里分享它们。我做了一些简单的“System.currentTimeMillis()”计算,看看我的修改是否有所改进,它们似乎比书中发现的要好一些。你能为这段代码提供更好的改进、解释或指标吗?

    public void test() {

    long time1 = System.currentTimeMillis();

    /*
     * From text, page 116
     */
    IntStream.rangeClosed(1,  100).boxed()
        .flatMap(a -> IntStream.rangeClosed(a, 100)
                .filter(b -> Math.sqrt(a*a + b*b) % 1 == 0)
                .mapToObj(b -> new int[]{a, b, (int)Math.sqrt(a*a + b*b)})
        )
        .forEach(c -> System.out.println("["+c[0]+" "+c[1]+" "+c[2]+"]"));

    long time2 = System.currentTimeMillis();

    System.out.println();

    long time3 = System.currentTimeMillis();

    /*
     * From text, page 117, I added "map(...)" so that end result are ints, not doubles
     */
    IntStream.rangeClosed(1, 100).boxed()
        .flatMap(a -> IntStream.rangeClosed(a, 100)
                .mapToObj(b -> new double[]{a, b, Math.sqrt(a*a + b*b)})
                .filter(t -> t[2] % 1 == 0)
                .map(b -> new int[]{(int)b[0], (int)b[1], (int)b[2]})
        )
        .forEach(c -> System.out.println("["+c[0]+" "+c[1]+" "+c[2]+"]"));

    long time4 = System.currentTimeMillis();

    System.out.println();

    long time5 = System.currentTimeMillis();

    /*
     * My optimization attempt #1: now mapToObj(...) has c%1!=0 conditional, filter checks array element not null
     */
    IntStream.rangeClosed(1, 100).boxed()
    .flatMap(a -> IntStream.rangeClosed(a, 100)
                .mapToObj(b -> {
                    double c = Math.sqrt(a*a + b*b);
                    return new Object[]{a, b, c % 1 == 0 ? (int)c : null};
                })
                .filter(d -> d[2] != null)
                .map(e -> new int[]{(int)e[0], (int)e[1], (int)e[2]})
    )
    .forEach(f -> System.out.println("["+f[0]+" "+f[1]+" "+f[2]));

    long time6 = System.currentTimeMillis();

    System.out.println();

    long time7 = System.currentTimeMillis();

    /*
     * My optimization attempt #2: now mapToObj(...) has c%1!=0 conditional, filter checks "array element" not 0
     */
    IntStream.rangeClosed(1, 100).boxed()
        .flatMap(a -> IntStream.rangeClosed(a, 100)
                .mapToObj(b -> {
                    double c = Math.sqrt(a*a + b*b);
                    return new int[]{a, b, c % 1 == 0 ? (int)c : 0 };
                })
                .filter(t -> t[2] != 0)
        )
        .forEach(d -> System.out.println("["+d[0]+" "+d[1]+" "+d[2]+"]"));

    long time8 = System.currentTimeMillis();

    System.out.println();

    long time9 = System.currentTimeMillis();

    /*
     * My optimization attempt #3: now mapToObj(...) has c%1!=0 conditional, filter checks "collection element" not null
     */
    IntStream.rangeClosed(1, 100).boxed()
        .flatMap(a -> IntStream.rangeClosed(a, 100)
                .mapToObj(b -> {
                    double c = Math.sqrt(a*a + b*b);
                    return (c % 1 != 0) ? null : new int[]{a, b, (int)c};
                })
                .filter(t -> t != null)
        )
        .forEach(d -> System.out.println("["+d[0]+" "+d[1]+" "+d[2]+"]"));

    long time10 = System.currentTimeMillis();

    System.out.println();

    long timeDelta1 = time2 - time1;
    long timeDelta2 = time4 - time3;
    long timeDelta3 = time6 - time5;
    long timeDelta4 = time8 - time7;
    long timeDelta5 = time10 - time9;

    System.out.println("timeDelta1: " + timeDelta1 + ", timeDelta2: " + timeDelta2 + ", timeDelta3: " + timeDelta3 + ", timeDelta4: " + timeDelta4 + ", timeDelta5: " + timeDelta5);

}

public static void main(String[] args){
    ReduceTest reduceTest = new ReduceTest();
    reduceTest.test();
}

注意:您似乎可以使用“return;” 在“.forEach()”方法中,但不在“.mapToInt()”方法中。使用“返回;” 在传递给“.mapToInt()”方法的 lambda 中,将不再需要使用“.filter()”方法。看起来这将是对流 api 的改进。

4

1 回答 1

3
于 2017-01-09T11:11:47.827 回答