首页 \ 问答 \ java优化怎么样?(How does java optimisation? Example code)

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
更新时间:2023-10-28 21:10

最满意答案

在第一个代码示例中,

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 to int, initialized to point to the first element of arr. But it can point to any 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*

相关问答

更多
  • 访问指针指向的项目有一个替代语法 - 方括号。 这个语法可以让你通过指针访问数据,就好像指针是一个数组(当然,指针不是数组)。 表达式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 (*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 ...
  • *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 ...
  • 您可以通过分割整个数组来对数组进行排序 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个 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)