数字流示例优化

时间:2017-01-08 18:44:33

标签: lambda functional-programming java-8 java-stream pythagorean

我正在阅读“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();
}

注意:您似乎可以使用“返回”;在“.forEach()”方法中,但不在“.mapToInt()”方法中。用“返回”在传入“.mapToInt()”方法的lambda中,不需要使用“.filter()”方法。这似乎是对流API的改进。

1 个答案:

答案 0 :(得分:3)

首先,你的“基准”存在严重缺陷。很可能总是看起来最后的变体更快,因为更多的共享代码(例如Stream API方法)在执行时已被编译/优化。见“How do I write a correct micro-benchmark in Java?”

除此之外,当您想测试一个数字是否为整数时,“%1”是不必要的。您可以转换为int,这将删除小数部分,并将其与原始double进行比较。此外,当您已经知道要打印的内容时,您不需要mapint[]数组String

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 -> ((int)t[2]) == t[2])
            .map(arr -> String.format("[%.0f %.0f %.0f]", arr[0], arr[1], arr[2]))
    )
    .forEach(System.out::println);

但是,当然,如果你知道你需要多次像sqrt那样昂贵的功能,那么提前计算它可能是有益的,特别是当有可能在不使用昂贵功能的情况下进行准备甚至浮点运算一般:

int[] square = new int[20001];
IntStream.rangeClosed(1, 141).forEach(i -> square[i*i]=i);
IntStream.rangeClosed(1, 100).boxed()
    .flatMap(a -> IntStream.rangeClosed(a, 100)
            .filter(b -> square[a*a+b*b]!=0)
            .mapToObj(b -> String.format("[%d %d %d]", a, b, square[a*a+b*b]))
    )
    .forEach(System.out::println);

请注意,这是少数几种情况之一,其中嵌套forEach可以替代flatMap

int[] square=new int[20001];
IntStream.rangeClosed(1, 141).forEach(i -> square[i*i]=i);
IntStream.rangeClosed(1, 100)
    .forEach(a -> IntStream.rangeClosed(a, 100)
            .filter(b -> square[a*a+b*b]!=0)
            .forEach(b -> System.out.printf("[%d %d %d]%n", a, b, square[a*a+b*b]))
    );