Java -内联代码有好处吗?

时间:2021-06-09 15:02:51

I've done a bit of research but I mostly see c++ answers. The closest I've come to is this. I also saw this page but it doesn't really explain anything.

我做了一些研究,但我主要看到c++的答案。离我最近的是这个。我也看到了这一页,但它并不能解释任何事情。

Are there any advantages if I use the second piece of code? Will there be noticable performance differences? What about memory? What if it's done repetitively?

如果我使用第二段代码有什么好处吗?会有明显的性能差异吗?记忆呢?如果是重复做呢?

Right now I have this function. I'm sure the benefit of this is code readability:

现在我有这个函数。我确信这样做的好处是代码可读性:

private static Bitmap resize(Bitmap image, int maxWidth) {
    float widthReducePercentage = ((float) maxWidth / image.getWidth());
    int scaledHeight = Math.round(image.getHeight() * widthReducePercentage);

    return Bitmap.createScaledBitmap(image, maxWidth, scaledHeight, true);
}

Now, I have this second snippet of code:

现在,我有第二段代码:

private static Bitmap resize(Bitmap image, int maxWidth) {
    return Bitmap.createScaledBitmap(image, maxWidth, Math.round(image.getHeight() * (float) maxWidth / image.getWidth()), true);
}

A simpler example would be:

一个更简单的例子是:

for(;;) {
    String foo = "hello";
    Console.print(foo + "world");
}

versus

for(;;) {
    Console.print("hello" + "world");
}

4 个解决方案

#1


9  

First: this is not what "inlining" means. See: What is inlining?

首先:这不是“内联”的意思。看:内联是什么?

Second: no, there won't be any measurable difference in performance. In both of your code examples, it's likely that the compiled code will be identical for both versions.

第二:不,在性能上没有任何可衡量的差异。在这两个代码示例中,编译后的代码可能对两个版本都是相同的。

#2


4  

I defined two simple classes Test1 and Test2 and compiled them.

我定义了两个简单的类Test1和Test2,并对它们进行了编译。

public class Test1{
    public String f(){
        String s = "Hello";
        String t = "There";
        return s + t;
    }
}

and

public class Test2{
    public String f(){
        return "Hello" + "There";
    }   
}

Much to my surprise, the .class files are NOT of the same size.

令我惊讶的是,.class文件的大小并不相同。

-rw-r--r--  1 csckzp  staff  426 Dec 23 19:43 Test1.class
-rw-r--r--  1 csckzp  staff  268 Dec 23 19:43 Test2.class

Perhaps I shouldn't be surprised, since some amount of symbolic info is stored along with code. I ran the .class files through an online decompiler. Test1 was reconstructed pretty much the way it was typed in. Test2, on the other hand, decompiled this way:

也许我不应该感到惊讶,因为一些符号信息与代码一起存储。我通过在线反编译器运行.class文件。Test1是按照输入的方式进行重构的。Test2则是这样反编译的:

public class Test2 {
    public String f() {
        return "HelloThere";
    }
}

The compiler's optimization clearly shows here. Perhaps there is a small penalty in Java for non-compact code.

编译器的优化在这里清楚地显示出来。对于非紧凑的代码,Java中可能会有一个小的惩罚。

#3


3  

They're both the same. the former just makes things clearer than the latter.

他们都是相同的。前者只是让事情比后者更清晰。

There are situations such as the block below that make one-liners useful.

有一些情况,比如下面的块,使一行程序变得有用。

public boolean isEmpty() {
   return getCount() != 0;
}

If you want to make it easier to read, especially when it comes to equations, go for a variable. one-liners make it simple and short, but are good for short and simple logics.

如果你想让它更容易阅读,特别是当它涉及到方程时,选择一个变量。一行程序简单而简短,但对简短的逻辑很好。

This is my personal opinion.

这是我个人的看法。

#4


1  

While local variables survive the translation to bytecode, they are very unlikely to survive just in time compilation. Moreover, even if the local variables were present, they would not significantly affect the performance of that method, as rescaling a bitmap is orders of magnitude more expensive than storing or retrieving a local variable.

虽然局部变量在字节码的转换中幸存下来,但是它们不太可能仅在时间编译中存活。此外,即使存在局部变量,也不会显著影响该方法的性能,因为重新绘制位图的开销要比存储或检索局部变量大很多。

Your second example about the string concatenation highlights a small exception to this rule, as the presence of local variables may inhibit compile time evaluation of the concatenation of constant strings. Again however, this is very unlikely to significant affect the runtime of your program, because you are probably not concatenating constant strings very often.

关于字符串连接的第二个示例突出显示了这个规则的一个小异常,因为本地变量的存在可能会抑制对常量字符串连接的编译时评估。然而,这也不太可能显著影响程序的运行时,因为您可能不经常连接常量字符串。

Generally speaking, the effect of inlining local variables on runtime performance is very rarely measurable, let alone significant. Therefore, your time is much better spent optimizing programmer performance by making your code easy to read and reason about.

一般来说,内联局部变量对运行时性能的影响很少是可度量的,更不用说显著的影响了。因此,通过使您的代码易于阅读和推理,您的时间花在优化程序员性能上的效果会好得多。

#1


9  

First: this is not what "inlining" means. See: What is inlining?

首先:这不是“内联”的意思。看:内联是什么?

Second: no, there won't be any measurable difference in performance. In both of your code examples, it's likely that the compiled code will be identical for both versions.

第二:不,在性能上没有任何可衡量的差异。在这两个代码示例中,编译后的代码可能对两个版本都是相同的。

#2


4  

I defined two simple classes Test1 and Test2 and compiled them.

我定义了两个简单的类Test1和Test2,并对它们进行了编译。

public class Test1{
    public String f(){
        String s = "Hello";
        String t = "There";
        return s + t;
    }
}

and

public class Test2{
    public String f(){
        return "Hello" + "There";
    }   
}

Much to my surprise, the .class files are NOT of the same size.

令我惊讶的是,.class文件的大小并不相同。

-rw-r--r--  1 csckzp  staff  426 Dec 23 19:43 Test1.class
-rw-r--r--  1 csckzp  staff  268 Dec 23 19:43 Test2.class

Perhaps I shouldn't be surprised, since some amount of symbolic info is stored along with code. I ran the .class files through an online decompiler. Test1 was reconstructed pretty much the way it was typed in. Test2, on the other hand, decompiled this way:

也许我不应该感到惊讶,因为一些符号信息与代码一起存储。我通过在线反编译器运行.class文件。Test1是按照输入的方式进行重构的。Test2则是这样反编译的:

public class Test2 {
    public String f() {
        return "HelloThere";
    }
}

The compiler's optimization clearly shows here. Perhaps there is a small penalty in Java for non-compact code.

编译器的优化在这里清楚地显示出来。对于非紧凑的代码,Java中可能会有一个小的惩罚。

#3


3  

They're both the same. the former just makes things clearer than the latter.

他们都是相同的。前者只是让事情比后者更清晰。

There are situations such as the block below that make one-liners useful.

有一些情况,比如下面的块,使一行程序变得有用。

public boolean isEmpty() {
   return getCount() != 0;
}

If you want to make it easier to read, especially when it comes to equations, go for a variable. one-liners make it simple and short, but are good for short and simple logics.

如果你想让它更容易阅读,特别是当它涉及到方程时,选择一个变量。一行程序简单而简短,但对简短的逻辑很好。

This is my personal opinion.

这是我个人的看法。

#4


1  

While local variables survive the translation to bytecode, they are very unlikely to survive just in time compilation. Moreover, even if the local variables were present, they would not significantly affect the performance of that method, as rescaling a bitmap is orders of magnitude more expensive than storing or retrieving a local variable.

虽然局部变量在字节码的转换中幸存下来,但是它们不太可能仅在时间编译中存活。此外,即使存在局部变量,也不会显著影响该方法的性能,因为重新绘制位图的开销要比存储或检索局部变量大很多。

Your second example about the string concatenation highlights a small exception to this rule, as the presence of local variables may inhibit compile time evaluation of the concatenation of constant strings. Again however, this is very unlikely to significant affect the runtime of your program, because you are probably not concatenating constant strings very often.

关于字符串连接的第二个示例突出显示了这个规则的一个小异常,因为本地变量的存在可能会抑制对常量字符串连接的编译时评估。然而,这也不太可能显著影响程序的运行时,因为您可能不经常连接常量字符串。

Generally speaking, the effect of inlining local variables on runtime performance is very rarely measurable, let alone significant. Therefore, your time is much better spent optimizing programmer performance by making your code easy to read and reason about.

一般来说,内联局部变量对运行时性能的影响很少是可度量的,更不用说显著的影响了。因此,通过使您的代码易于阅读和推理,您的时间花在优化程序员性能上的效果会好得多。