java优化怎么样?(How does java optimisation? Example code)
考虑这个小班:
public class Example { public int plus(int a){ int b; b = 1; return b+a; } }
javap说:
public int plus(int); descriptor: (I)I flags: ACC_PUBLIC Code: stack=2, locals=3, args_size=2 0: iconst_1 1: istore_2 2: iload_2 3: iload_1 4: iadd 5: ireturn LineNumberTable: line 4: 0 line 5: 2
我不明白为什么我们需要第1和第2行?
我听说Java Runtime环境会计算函数调用并在超过一定数量的调用时编译函数。
这是否意味着Java编译器将所有优化都留给了字节码编译器? 例如Java程序开始变慢并且随着时间变得更快?
请解释!
Consider this small class:
public class Example { public int plus(int a){ int b; b = 1; return b+a; } }
javap says:
public int plus(int); descriptor: (I)I flags: ACC_PUBLIC Code: stack=2, locals=3, args_size=2 0: iconst_1 1: istore_2 2: iload_2 3: iload_1 4: iadd 5: ireturn LineNumberTable: line 4: 0 line 5: 2
I don't understand why we need line 1&2?
I heard that the Java Runtime environment counts function calls and compiles functions if they exceed a certain number of calls.
Does that mean that the Java compiler leaves all optimisation to the byte code compiler? E.g. Java programs start slow and become faster by time?
Please explain!
原文:https://stackoverflow.com/questions/43119920
最满意答案
在第一个代码示例中,
int (*Parr)[10] = &arr;
Parr
是指向10个int的数组的指针。 它只能指向这样一个对象。 例如,int (*Parr)[10]; int a[10]; Parr = &a; // OK int b[42]; Parr = &b; // ERROR, b is of the wrong type
在第二个代码示例中,
int *Parr = arr;
Parr
是指向int
的指针,初始化为指向arr
的第一个元素。 但它可以指向任何int
。int* Parr; int a[10]; Parr = &a; // OK, a decays to int* int b[42]; Parr = &b; // OK, b decays to int* int c = 42; Parr = &c; // OK, c is an int*
In the first code sample,
int (*Parr)[10] = &arr;
Parr
is a pointer to an array of 10 ints. It can only point to such an object. For example,int (*Parr)[10]; int a[10]; Parr = &a; // OK int b[42]; Parr = &b; // ERROR, b is of the wrong type
In the second code sample,
int *Parr = arr;
Parr
is a pointer toint
, initialized to point to the first element ofarr
. But it can point to anyint
.int* Parr; int a[10]; Parr = &a; // OK, a decays to int* int b[42]; Parr = &b; // OK, b decays to int* int c = 42; Parr = &c; // OK, c is an int*
相关问答
更多-
访问指针指向的项目有一个替代语法 - 方括号。 这个语法可以让你通过指针访问数据,就好像指针是一个数组(当然,指针不是数组)。 表达式a[i]只是写作*(a+i) *的替代形式。 当您分配动态存储并将其分配给myInt ,您可以使用类似于动态数组的指针,该数组可以在运行时更改大小: myInt = malloc(1024*sizeof(int)); // You do not need a cast in C, only in C++ for (int i = 0 ; i != 1024 ; i++) { ...
-
指向一组int的指针(Pointer to an array of ints)[2023-08-21]
在第一个代码示例中, int (*Parr)[10] = &arr; Parr是指向10个int的数组的指针。 它只能指向这样一个对象。 例如, int (*Parr)[10]; int a[10]; Parr = &a; // OK int b[42]; Parr = &b; // ERROR, b is of the wrong type 在第二个代码示例中, int *Parr = arr; Parr是指向int的指针,初始化为指向arr的第一个元素。 但它可以指向任何int 。 int* Pa ... -
指向ints c ++数组的指针(pointer to array of ints c++)[2022-03-06]
*arrPtr或arrPtr[0]产生int[10]类型的对象 用于operator <<将数组显式转换为int *类型的指针,并为此类表达式选择重载operator <<用于const void *的参数。 你可以看到这些输出 arr: 0xbf843e28 和 *arrPtr: 0xbf843e28 arrPtr[0]: 0xbf843e28 重合。 如果你想输出数组的第一个元素,你应该写出 std::cout << **arrPtr << std::endl; std::cout << ( *ar ... -
你的平台不提供intptr_t吗? Yet another solution: #define BIG_ENOUGH 4 typedef union { int buffer[BIG_ENOUGH]; pointer_t* pointer; } proxy_t; static_assert(sizeof(pointer_t*) <= BIG_ENOUGH*sizeof(int)); // before calling the function proxy_t proxy; proxy.p ...
-
对Ints数组进行排序(Sorting array of Ints)[2023-04-26]
您可以通过分割整个数组来对数组进行排序 sort.Ints(array[:]) 但是,你可能不想首先使用数组,而应该使用[]int片。 此外,您的sortedArray与array值相同,因此没有理由创建第二个变量。 You can sort an array by taking a slice of the entire array sort.Ints(array[:]) You probably don't want an array in the first place however, and ... -
>>> platform.architecture() ('64bit', 'WindowsPE') >>> ctypes.sizeof(ctypes.c_int*4) 16 >>> ctypes.sizeof(ctypes.POINTER(ctypes.c_int)) 8 显然,这些是不同的实体。 具体来说,不能从C函数返回数组,因为它不适合寄存器。 在第二种情况下,你的结果是你的返回int*被ctypes保存到某处并被解释为int[4] :第一个是你的指针,还有一些垃圾在以下地址。 在x64上,第一个 ...
-
从概念上讲,它们不是一回事。 即使他们指向相同的地址,它们是不兼容的指针类型,因此它们的用法也不同。 &a正在使用int [4]类型的数组的地址),那么它意味着一个指向数组的指针(即int (*)[4] )。 a会导致数组到指针的衰减 ,那么它意味着一个指向int的指针(即int* )。 Conceptually they're not the same thing. Even they point to the same address, they're incompatible pointer type ...
-
for (ptr; *ptr < *(ptr + 4); ptr++) /* after ptr is incremented (ptr+4) is again calculated in iterations */ 在第一次迭代后的这个循环中, *(ptr+4)将超出边界 ( 因为它在每次迭代中计算 ),因为你增加ptr并且取消引用它会导致未定义的行为 。 同样的事情发生在下一次迭代中。 上面的情况不会发生在后一种情况下,因为end保持不变,并且随着ptr递增而不随ptr而变化,因此它起作用。 ...
-
你说你必须使用逗号分隔的一行文本,但实际上你已经声明了一个包含十个(二进制)整数的char数组。 要将其转换为字符串,您只需执行此操作: char str[] = "1,2,3,4,5,6,7,8,9,10"; 然后你需要处理这个字符串来获取每个数字并进入你的int数组。 You said that you have to use a line of text separated by commas but you've actually declared a char array containing t ...
-
当你声明一个变量时 int array[137]; 变量array的类型是int[137] ; 也就是说,一个137 int的数组。 在C ++中,您可以创建指向您想要的任何类型的变量的指针或引用,因此您可以创建指向变量array的指针。 由于array类型为“137 int s的array ”,因此指向数组的指针将具有“指向137 int s数组的指针”。 写出这样的指针的语法是 int (*arrayPtr)[137] = &array; 这被解读为“ arrayPtr是一个指针,它指向的是137个 ...