Are variable definitions that are used once optimized?

前端 未结 2 1602
温柔的废话
温柔的废话 2021-01-18 03:36

Consider the following method:

private static long maskAndNegate(long l) {
    int numberOfLeadingZeros = Long.numberOfLeadingZeros(l)
    long mask = Cust         


        
2条回答
  •  余生分开走
    2021-01-18 03:44

    The code is not significantly large enough to be optimized for starters. And in the second way you are just saving the memory used for storing references to numberOfLeadingZeros and all.

    But when you will use this code significantly enough on runtime such as 10000 times at least, then JIT will identify it as HOT code and then optimize it with neat tricks such as Method Inlining and similar sorts.

    But in your case preferable option is first one as it is more readable.

    You should not compromise Readability for small optimizations.

提交回复
热议问题