亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java - 使用lambda表達(dá)式(匿名內(nèi)部類)傳參是否有性能問(wèn)題?
阿神
阿神 2017-04-18 10:53:20
0
3
1218

編程有一條原則如下:

避免創(chuàng)建不必要的對(duì)象:最好能重用對(duì)象,而不要在每次需要的時(shí)候就創(chuàng)建一個(gè)相同功能的新對(duì)象。

請(qǐng)看如下代碼:

List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");

 // 1、匿名內(nèi)部類
Collections.sort(names, new Comparator<String>() {
    @Override
    public int compare(String a, String b) {
        return b.compareTo(a);
    }
});

//2、lambda表達(dá)式
Collections.sort(names, (a, b) -> b.compareTo(a));

是不是每次排序都創(chuàng)建了一個(gè)新的Comparator對(duì)象,導(dǎo)致性能降低?那么還主張使用Lambda表達(dá)式嗎?

阿神
阿神

閉關(guān)修行中......

reply all(3)
Peter_Zhu

First answer your question:
Is a new Comparator object created every time sorting, resulting in reduced performance? So do you still advocate using Lambda expressions?
No, claim

  1. The fundamental reason
    Lamdba expression is not syntactic sugar for anonymous inner classes at all. That is to say, the underlying implementation of Lambda expression is not the implementation of anonymous inner classes. They are actually two things

  2. How to prove it?
    Anonymous inner classes will actually generate a class file during compilation, named with ClassName$ numbers, so if the bottom layer of the Lamdba expression is also implemented by anonymous inner classes, a similarly similar inner file will definitely be generated.
    So we simply write your example in classes under different packages, and then check the compiled effect
    Anonymous inner class implements InnerTest


    This is a class file, you can see there are two

    lambda expression LamdbaTest

    However, there is only one class file


    So there is only one truth, haha, obviously, this is not the same thing at all

  3. How is Lamdba expression implemented?
    We can take a look at their bytecode, so that the underlying differences will be fully demonstrated
    InnerTest

    LamdbaTest

    You can see that the instructions used by the two are different. When parsing the Lamdba expression, it uses the new invokedynamic instruction in Java 7. If you are familiar with this instruction, you may understand it instantly. If you are not familiar with it, you may understand it instantly. , you can ask Du Niang
    But you can understand it directly from the name of this command: Dynamic call
    Therefore, unlike anonymous inner classes that are compiled into a class file, Lamdba expressions are compiled into a static method , we can see it again by looking at -p. The generated method is called lambda$main

    ??
所以結(jié)合之前兩個(gè)反編譯的結(jié)果可以看到,lamdba表達(dá)式運(yùn)行整體思路大致如下  
    1. lamdba表達(dá)式被編譯生成當(dāng)前類的一個(gè)私有靜態(tài)方法  
    2. 在原調(diào)用Lamdba方法的地方編譯成了一個(gè)invokedynamic指令調(diào)用,同時(shí)呢也生成了一個(gè)對(duì)應(yīng)的BootstrapMethod  
    3. 當(dāng)lamdba表達(dá)式被JVM執(zhí)行,也就是碰到2中說(shuō)到的invokedynamic指令,該指令引導(dǎo)調(diào)用LambdaMetafactory.metafactory方法,該方法返回一個(gè)CallSite實(shí)例
    4. 而這個(gè)CallSite實(shí)例中的target對(duì)象,也就是直接引用到一個(gè)MethodHandle實(shí)例,而這個(gè)MethodHandle實(shí)例會(huì)調(diào)用到1中生成的靜態(tài)方法,在上面的例子就是`lambda$mainrrreee`這個(gè)方法,完成整個(gè)lamdba表達(dá)式的使用  
  1. One more thing
    In fact, you can see that lamdba expressions are indeed much better than anonymous inner classes in a sense, whether it is performance, readability or general trend~ Haha, I want to say general trend because lamdba expressions can be used later There is wider room for optimization. Anyway, I am used to using it in java and I like it very much

阿神
  1. Each sorting does not create a new Comparator object. Generally, once the instance corresponding to the Lambda expression is generated in memory, the JVM will reuse this instance in the current environment. For specific instructions, you can refer to this link: Run-Time Evaluation of Lambda Expressions

  2. On the premise that it can improve program readability and development efficiency, it is recommended to use Lambda expression

劉奇
  1. You can statically declare an anonymous class object inside a Lambda expression;

  2. Small objects that come and go are not a big problem for the JVM;

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template