在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?

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

问题描述

我注意到我总是使用 int 和 doubles,无论数字需要多小或多大.那么在java中,使用byteshort代替intfloat代替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?

假设我有一个包含大量整数和双精度数的程序.如果我知道数字适合,是否值得将我的整数更改为字节或短裤?

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 位原始单元大小的倍数对堆栈和对象字段进行建模.因此,当您将局部变量或对象字段声明为(例如)byte 时,变量/字段将存储在 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.

这有两个例外:

  • longdouble 值需要 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.

所以它可能值得优化使用 longdouble ... 以及大型基元数组.但一般不会.

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 答案中的基准测试结果,似乎使用 shortbyte 而不是 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 consider them in isolation ... but that's another topic.)

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

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.)

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

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 没有无符号类型,但是如果我知道数字只会是正数,我还能做些什么吗?

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?

没有.无论如何都不是在性能方面.(IntegerLong 等中有一些方法可以将 intlong 等处理为无符号.但是这些并没有带来任何性能优势.这不是它们的目的.)

No. Not in terms of performance anyway. (There are some methods in Integer, Long, etc for dealing with int, long, etc as unsigned. But these don't give any performance advantage. That is not their purpose.)

(我假设垃圾收集器只处理对象而不是原始对象,但仍会删除废弃对象中的所有原始对象,对吗?)

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

正确.对象的字段是对象的一部分.当对象被垃圾收集时它就会消失.同样,当数组被收集时,数组的单元格就会消失.当字段或单元格类型是原始类型时,则值存储在字段/单元格中......这是对象/数组的一部分......并且已被删除.

Correct. A field of an object is part of the object. It goes away when the object is garbage collected. Likewise the cells of an array go away when the array is collected. When the field or cell type is a primitive type, then the value is stored in the field / cell ... which is part of the object / array ... and that has been deleted.

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

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