Java中的字节和短点(我已经阅读了其他问题) [英] point of Byte and Short in Java (I've read the other questions)

查看:65
本文介绍了Java中的字节和短点(我已经阅读了其他问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:
如果我从Java反汇编中得到正确的答案,当我使用

My question is: If I got it right from the Java disassembly, when I use

byte a=3,b=5;
System.out.println(a+b);

实际上将使用int而不是字节。同样,所有本地内存插槽也都是4B,就像堆栈插槽一样。我意识到分配字节数组可能会更有效,但是使用单个字节值最终会导致效率低下吗? (简称同一点)。

would actually use int instead of byte. Also all local memory slots are 4B just as stack slots. I realize that allocating a byte array would probably act more efficiently, but is it true that using a single byte value is ultimately inefficient? (The same point for short)

推荐答案

性能调整的第一个规则应该是编写简单,清晰的代码。

The first rule of performance tuning should be to write simple, clear code.

在此示例中,没有性能差异,即使有println()花费10,000倍以上的时间来做任何名义上的差异。

In this example, there is no performance difference and even if there were the println() takes 10,000x times longer making any difference notional.

程序在字节码中的显示方式与在本机代码中的显示方式不同。

How a program appears in byte-code and how it appears in native code is different.

不是JVM中所有插槽均为4字节。例如在64位计算机上的引用可以是8个字节,但仍使用一个插槽

Not all slots are 4-bytes in the JVM. e.g. a reference on a 64-bit machine can be 8-bytes but it still uses one "slot"

您的计算机没有插槽。它确实具有通常为32位或64位的寄存器。

Your machine doesn't have slots. It does have registers which are typically 32-bit or 64-bit.

在您的示例中,使用了字节操作,该操作与int操作一样有效,并且可以产生不同的结果,因此仍然需要它们。

In your example, byte operations are used, which are just as efficient as int operations, and can produce a different result so they are still required.

注意:具有 byte short 字段的对象可以小于一个带有 int 字段。

Note: an object with byte or short fields can be smaller than one with a int fields.

在此示例中,JVM可以计算 c 一次,因此不需要 a b

In this example, the JVM can calculate c once so it doesn't need a or b

这篇关于Java中的字节和短点(我已经阅读了其他问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆