在java中,使用byte或short代替int和float而不是double更有效吗? [英] In java, is it more efficient to use byte or short instead of int and float instead of double?

查看:180
本文介绍了在java中,使用byte或short代替int和float而不是double更有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到无论数量有多大或多大,我总是使用int和double。所以在java中,使用 byte short 代替 int float 而不是 double

I've noticed I've always used int and doubles no matter how small or big the number needs to be. So in java, is it more efficient to use byte or short instead of int and float instead of double?

因此假设我有一个有大量整数和双精度的程序。如果我知道这个数字适合,是否值得通过并将我的int更改为字节或短裤?

So assume I have a program with plenty of ints and doubles. Would it be worth going through and changing my ints to bytes or shorts if I knew the number would fit?

我知道java没有无符号类型但是有什么如果我知道这个数字只是正数,我可以做多余的事情?

I know java doesn't have unsigned types but is there anything extra I could do if I knew the number would be positive only?

通过高效我主要是指处理。如果所有变量都是一半大小并且计算可能会稍快一些,我认为垃圾收集器会快得多。
(我想因为我正在研究android我需要有点担心ram)

By efficient I mostly mean processing. I'd assume the garbage collector would be a lot faster if all the variables would be half size and that calculations would probably be somewhat faster too. ( I guess since I am working on android I need to somewhat worry about ram too)

(我假设垃圾收集器只处理对象和不是原始的,但仍然删除被遗弃物体中的所有原语吗?)

(I'd assume the garbage collector only deals with Objects and not primitive but still deletes all the primitives in abandoned objects right? )

我尝试了一个小的Android应用程序,但我根本没有发现任何差异。 (虽然我没有科学地衡量任何东西。)

I tried it with a small android app I have but didn't really notice a difference at all. (Though I didn't "scientifically" measure anything.)

假设它应该更快更高效我错了吗?我讨厌通过并改变大规模计划中的所有内容,以发现我浪费了我的时间。

Am I wrong in assuming it should be faster and more efficient? I'd hate to go through and change everything in a massive program to find out I wasted my time.

当我开始新的时候从一开始就值得做项目? (我的意思是我认为每一点都会有所帮助,但如果是这样的话,为什么看起来没有人会这样做。)

Would it be worth doing from the beginning when I start a new project? (I mean I think every little bit would help but then again if so, why doesn't it seem like anyone does it.)

推荐答案


假设它应该更快更有效,我错了吗?我讨厌通过并改变一个大型程序中的所有内容,以发现我浪费了我的时间。

Am I wrong in assuming it should be faster and more efficient? I'd hate to go through and change everything in a massive program to find out I wasted my time.



简短回答



是的,你错了。在大多数情况下,它在使用的空间方面差别很小

除非您有明确证据表明需要进行优化,否则不值得尝试对此进行优化。如果你需要来优化对象字段的内存使用,你可能需要采取其他(更有效)的措施。

It is not worth trying to optimize this ... unless you have clear evidence that optimization is needed. And if you do need to optimize memory usage of object fields in particular, you will probably need to take other (more effective) measures.

Java虚拟机使用(实际上)32位原始单元格大小的倍数的偏移来建模堆栈和对象字段。因此,当您将局部变量或对象字段声明为(例如)字节时,变量/字段将存储在32位单元格中,就像 INT

The Java Virtual Machine models stacks and object fields using offsets that are (in effect) multiples of a 32 bit primitive cell size. So when you declare a local variable or object field as (say) a byte, the variable / field will be stored in a 32 bit cell, just like an int.

这有两个例外:


  • long double 值需要2个原始32位单元格

  • 基本类型数组在包装中表示形式,以便(例如)一个字节数组每32位字保存4个字节。

  • long and double values require 2 primitive 32-bit cells
  • arrays of primitive types are represent in packed form, so that (for example) an array of bytes hold 4 bytes per 32bit word.

所以可能值得优化使用 long double ...以及大型基元数组。但总的来说没有。

So it might be worth optimizing use of long and double ... and large arrays of primitives. But in general no.

从理论上讲,JIT 可能能够对此进行优化,但实际上我从未听说过这样做的JIT。一个障碍是JIT通常在创建了正在编译的类的实例之后才能运行。如果JIT优化了内存布局,你可能会有两个(或更多)同类对象的风味......这会带来很大的困难。

In theory, a JIT might be able to optimize this, but in practice I've never heard of a JIT that does. One impediment is that the JIT typically cannot run until after there instances of the class being compiled have been created. If the JIT optimized the memory layout, you could have two (or more) "flavors" of object of the same class ... and that would present huge difficulties.

查看@ meriton的答案中的基准测试结果,似乎使用 short byte 而不是 int 会导致乘法性能下降。实际上,如果你单独考虑这些操作,那么惩罚就很重要。 (你不应该......但这是另一回事。)

Looking at the benchmark results in @meriton's answer, it appears that using short and byte instead of int incurs a performance penalty for multiplication. Indeed, if you consider the operations in isolation, the penalty is significant. (You shouldn't ... but that's another matter.)

我认为解释是JIT可能在每种情况下使用32位乘法指令进行乘法运算。但是在字节的情况下,它会执行额外的指令来转换中间体32在每次循环迭代中,位值为字节 short 。 (理论上,转换可以在循环结束时完成一次......但我怀疑优化器是否能够解决这个问题。)

I think the explanation is that JIT is probably doing the multiplications using 32bit multiply instructions in each case. But in the byte and short case, it executes extra instructions to convert the intermediate 32 bit value to a byte or short in each loop iteration. (In theory, that conversion could be done once at the end of the loop ... but I doubt that the optimizer would be able to figure that out.)

无论如何,这确实指出了切换到字节作为优化的另一个问题。在算术和计算密集的算法中,它可能会使性能更糟 ....

Anyway, this does point to another problem with switching to short and byte as an optimization. It could make performance worse ... in an algorithm that is arithmetic and compute intensive.

这篇关于在java中,使用byte或short代替int和float而不是double更有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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