我一直试图理解这意味着什么:
内联函数
在C++中,类声明中定义的成员函数.(2)编译器替换函数实际代码的函数调用.关键字inline可用于提示编译器执行成员或非成员函数体的内联扩展.
排队
在编译期间用函数代码的副本替换函数调用.
例如,它写的类似于:
当方法是最终的时,它可以是内联的.
这里:http://www.roseindia.net/javatutorials/final_methods.shtml
你能给我一个例子或者什么,或者基本上帮助我理解"它可能被内联"的含义.
谢谢.
在C++中,我可以声明一个方法"内联",编译器可能会内联它.据我所知,Java中没有这样的关键字.
如果JVM决定这样做,内联就完成了吗?我能以某种方式影响这个决定吗?
我做了一些研究,但我主要看到c ++的答案.我最接近的就是这个.我也看过这个页面,但它并没有真正解释任何事情.
如果我使用第二段代码有什么好处吗?会有明显的性能差异吗?记忆呢?如果重复完成怎么办?
现在我有这个功能.我确信这样做的好处是代码可读性:
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);
}
Run Code Online (Sandbox Code Playgroud)
现在,我有第二段代码:
private static Bitmap resize(Bitmap image, int maxWidth) {
return Bitmap.createScaledBitmap(image, maxWidth, Math.round(image.getHeight() * (float) maxWidth / image.getWidth()), true);
}
Run Code Online (Sandbox Code Playgroud)
一个更简单的例子是:
for(;;) {
String foo = "hello";
Console.print(foo + "world");
}
Run Code Online (Sandbox Code Playgroud)
与
for(;;) {
Console.print("hello" + "world");
}
Run Code Online (Sandbox Code Playgroud) 我从得知这个答案上for
和while
在C#中循环,即:“编译/ JIT有这种情况,只要你使用最佳化arr.Length
的状态:”
for(int i = 0 ; i < arr.Length ; i++) {
Console.WriteLine(arr[i]); // skips bounds check
}
Run Code Online (Sandbox Code Playgroud)
这让我想知道 java 编译器是否有这样的优化。
for(int i=0; i<arr.length; i++) {
System.out.println(arr[i]); // is bounds check skipped here?
}
Run Code Online (Sandbox Code Playgroud)
我认为是的,嗯,是吗?使用Collection
like时会发生同样的情况ArrayList
吗?
但是如果我必须在 for 循环myList.size()
体内部使用 的值,考虑现在myList
是一个 ArrayList 怎么办?那么在这种情况下不会提升 myList.size()
帮助,因为size()
是方法调用?例如可能是这样的:
int len = myList.size(); // hoisting for using inside the loop
for(int i = 0; …
Run Code Online (Sandbox Code Playgroud)