简体   繁体   English

使用更多方法是否意味着代码性能较低?

[英]Do more methods mean the code is less performant?

Do more methods, even if they are not called, have an affect on the performance of a particular class... 即使不调用更多方法,它们也会影响特定类的性能...

By performance, I mean anything, like does it take longer to create the object, does it take longer to actually execute a method...etc... 就性能而言,我的意思是什么,例如创建对象需要花费更长的时间,实际执行方法需要花费更长的时间...等等...

Now my understanding is that the code will only be compiled by the JIT compiler if it reaches a code block/method that it has not reached before...which would lead me to think that I am no affecting anything by adding default methods. 现在,我的理解是,只有在到达之前未达到的代码块/方法的情况下,该代码才由JIT编译器进行编译...这将使我认为我不会通过添加默认方法来影响任何事情。 Yes it will add to the "size" of the (byte) code but doesn't actually affect performance? 是的,它将增加(字节)代码的“大小”,但实际上不影响性能吗?

Am I right or wrong? 我是对还是错?

Here is the example: 这是示例:

public interface MyInterface {
    void someMethod();

    public default void myDefaultMethod() {
        System.out.println("hey");
    }
}

public class MyClass implements MyInterface {

    public void someMethod() {
    }
}

public static void main(String[] args) {
    MyClass c = new MyClass();
    c.someMethod();
    c.myDefaultMethod();
}

If I then change MyInterface and add LOTS of default methods, even if they are never called, will it have an affect: 如果我随后更改MyInterface并添加很多默认方法,即使它们从未被调用过,也会产生影响:

public interface MyInterface {
    void someMethod();

    public default void myDefaultMethod() {
        System.out.println("hey");
    }

    public default void myDefaultMethod1() {
        System.out.println("hey1");
    }

    public default void myDefaultMethod2() {
        System.out.println("hey1");
    }

    // ...

    public default void myDefaultMethod100() {
        System.out.println("hey100");
    }
}

You're right in one sense. 从某种意义上说你是对的。 There's some nuance, though. 不过有些细微差别。

Do more methods, even if they are not called, have an affect on the performance of a particular class... 即使不调用更多方法,它们也会影响特定类的性能...

"Performance" usually refers to speed of execution of the program. “性能”通常是指程序的执行速度。 Code that is never executed will never (directly) consume any CPU time. 永远不会执行的代码永远不会(直接)消耗任何CPU时间。 Thus, code that is never executed cannot (directly) affect execution time. 因此,永远不会执行的代码不能(直接)影响执行时间。 So in that sense, you are correct. 所以从这个意义上讲,您是正确的。

By performance, I mean anything, like does it take longer to create the object, does it take longer to actually execute a method...etc... 就性能而言,我的意思是什么,例如创建对象需要花费更长的时间,实际执行方法需要花费更长的时间...等等...

No, and no. 不,不。 There's no reason having extra methods lying around would affect object creation time, as that's a function of object size, and at least in Java objects don't directly contain their methods, if memory serves. 没有理由使用额外的方法来影响对象的创建时间,因为这是对象大小的函数,并且至少在Java对象中,如果有内存,则对象不直接包含其方法。 Having extra methods definitely won't (directly) affect execution of unrelated methods. 拥有额外的方法绝对不会(直接)影响不相关方法的执行。

Now my understanding is that the code will only be compiled by the JIT compiler if it reaches a code block/method that it has not reached before... 现在我的理解是,只有在达到以前从未达到的代码块/方法的情况下,JIT编译器才能编译该代码。

This isn't totally right. 这是不完全正确的。 The JITC can revisit the same section of code over and over again if it determines that doing so would be beneficial. 如果JITC认为这样做会是有益的,则可以一遍又一遍地重新访问同一段代码。

... which would lead me to think that I am no affecting anything by adding default methods. ...这会使我认为我不会通过添加默认方法来影响任何事情。 Yes it will add to the "size" of the (byte) code but doesn't actually affect performance? 是的,它将增加(字节)代码的“大小”,但实际上不影响性能吗?

You're right that the bytecode file would be larger. 没错,字节码文件会更大。 You're wrong in that that wouldn't make a difference. 你错了,那不会有什么不同。

Code size can have a significant impact on performance. 代码大小可能会对性能产生重大影响。 Small programs that can fit mostly/entirely in cache will have a significant advantage over larger programs, as pieces of code don't have to be loaded from RAM or the HDD/SSD, which are much slower than the cache. 可以容纳大多/完全在高速缓存小程序将有显著优势较大的程序,如代码段不必从RAM或HDD / SSD,它比高速缓存慢得多装载。

The amount of code needed to do this might be pretty large, though, so maybe for a method or two it wouldn't matter that much. 但是,执行此操作所需的代码量可能非常大,因此对于一个或两个方法而言,它并不重要。 I'm not sure at what point code size in Java becomes a problem. 我不确定Java中的代码大小会在什么时候成为问题。 Never tried to find out. 从未尝试找出。

If you never call those methods, it might be possible that the bits of code that make up those methods are never loaded, which removes their cache-related performance penalty. 如果您从不调用这些方法,则可能永远不会加载组成这些方法的代码位,从而消除了与缓存有关的性能损失。 I'm not certain if splitting the program code like this is possible, though. 不过,我不确定是否可以拆分这样的程序代码。


So in the end, it probably wouldn't be harmful, so long as you don't have an excessive number of methods. 所以最后,只要您没有过多的方法,它可能就不会有害。 Having methods around that are never called, though, might be problem for code maintainability, which is always a factor that you should consider. 但是,永远不要调用周围的方法可能是代码可维护性的问题,这始终是您应考虑的因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 O(n ^ 2)中的性能调整代码更具性能 - Performance tune code in O (n^2) to be more performant 我如何优化此代码以看起来更高效? - how i can optimize this code to look more performant? RLE - 我的代码产生的更多而不是更少? - RLE - my code produced more instead less? Java多线程 - 更少或更少线程做更多的线程? - Java Multithreading - More Threads That Do Less, or Fewer Threads that Do More? 有没有更有效(和更少乏味)的方式来做到这一点? - Is there eny more efficient (and less tedious) way to do this? 如何使RegEx更具性能? - How to make RegEx more performant? 性能更好的equal或instanceof是什么? - what is more performant equal or instanceof? Runtime类中的内存方法是什么意思? - What do the memory methods in the Runtime class mean? 有没有一种方法可以在围绕连接点的AOP之前进行一次性处理,以使我的方面更加高效? - Is there a way to do one-time processing before an AOP around joinpoint to make my aspect more performant? 用Java为收银员编写代码。 使用math.round可以给我带来更多的收益,而使用math.floor有时会给我带来更少的收益? - Make a code for cashier in java. With math.round it gives me more and with math.floor sometimes less what i should do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM