JVM中64位整数的效率是否低于32位整数?

时间:2022-09-01 09:57:50

Background: I want to store numbers that are precise to 4 decimal places, without roundoff. So I thought of using integers internally; for example, 12.3456 is represented as 123456 internally. But with 32b integers, I can count only upto 214748, which is very small.

背景:我想存储精确到4位小数的数字,而不是舍入。所以我想在内部使用整数;例如,12.3456在内部表示为123456。但是对于32b整数,我只计算高达214748,这非常小。

I guess that 64-bit integers are the solution. But are operations involving 64-bit integers less efficient than 32-bit integers, given a machine running a 64-bit JVM?

我想64位整数是解决方案。但是,如果运行64位JVM的机器,涉及64位整数的操作效率低于32位整数吗?


BTW, I am using an information retrieval package (Solr), an optimization package (Drools) and other packages written in Java, and they may not play well with decimal datatype (if you suggest it).

顺便说一下,我正在使用信息检索包(Solr),优化包(Drools)和其他用Java编写的包,它们可能无法很好地使用十进制数据类型(如果你建议的话)。

3 个解决方案

#1


2  

Even if it is slower, I doubt this would be the bottleneck in your system. You are very likely going to have more significant performance issues in other parts of your program.

即使速度较慢,我也怀疑这会是你系统的瓶颈。您很可能会在程序的其他部分出现更严重的性能问题。

Also, the answer to this question provides more details, but basically "It's platform dependent.". It's not necessarily true 64 bit will be slower than 32 bit.

此外,这个问题的答案提供了更多细节,但基本上“它依赖于平台。”。 64位将比32位慢,这不一定是真的。

#2


1  

This is likely to be platform dependant. I have seen cases where using long instead of int is about 10% faster. The 64-bit JVM for Java 5.0 was about 5% - 10% slower than the 32-bit JVM for Java 5.0. Java 6 doesn't appear to have this problem.

这很可能取决于平台。我见过使用long而不是int的情况快了大约10%。用于Java 5.0的64位JVM比用于Java 5.0的32位JVM慢约5%-10%。 Java 6似乎没有这个问题。

I imagine the cost of dividing by 10000 far outweighs the cost of using a long instead of an int value.

我认为除以10000的成本远远超过使用long而不是int值的成本。

You could also use double, rounding the result to four decimal places before printing/outputting it.

您也可以使用double,在打印/输出结果之前将结果四舍五入到四位小数。

#3


1  

Generally, the more data you need to hurl around, the slower it is, so even on a 64-bit VM sticking to int instead of long is faster in most cases.

一般来说,你需要投入的数据越多,它就越慢,因此即使在64位虚拟机上坚持使用int而不是long也会在大多数情况下更快。

This becomes very clear if you think in terms of memory footprint: an array of 1 million ints requires 4MB, 1M longs eat 8MB.

如果从内存占用方面考虑,这一点就变得非常清楚:一百万个整数的阵列需要4MB,1M长就需要8MB。

As for the computational speed, there is some overhead to perform operations on 64-bit types with 32-bit instructions. But even if the VM can use 64-bit instructions (which it should on a 64-bit VM), depending on the CPU they may still be slower than their 32-bit counterparts (add/subtract will probably go in one clock, but multiply and divide in 64-bit are usually slower than in 32-bit).

至于计算速度,使用32位指令对64位类型执行操作会有一些开销。但即使VM可以使用64位指令(它应该在64位VM上),取决于CPU,它们可能仍然比它们的32位速率慢(加/减可能会在一个时钟内,但是64位乘法和除法通常比32位慢。


A very common misconception is that integer math is faster than floating point math. As soon as you need to perform extra operations to "normalize" your integers, floating point will beat your integer implementation flat in performance. The actual differences in clock cycles spent between integer and floating point instructions is neglible for most applications, so if floating point is waht you need, use it and don't attempt to emulate it yourself.

一个非常常见的误解是整数数学比浮点数学更快。一旦你需要执行额外的操作来“归一化”你的整数,浮点数就会在性能上超过你的整数实现。对于大多数应用程序,在整数和浮点指令之间花费的时钟周期的实际差异是可以忽略的,因此如果您需要浮动点,请使用它并且不要尝试自己模拟它。

For the question which type to actually use: Use the type thats most appropriate in terms of data representation. Worry about performance when you get there. Look at wht operations you need to perform and what precision you need. Then select the type that offers exactly that. Judging by the libraries you mentioned, double will probably be the winner of that.

对于实际使用哪种类型的问题:使用最适合数据表示的类型。当你到达那里时担心性能。查看您需要执行的操作以及所需的精度。然后选择提供该类型的类型。从您提到的图书馆来看,双倍可能会成为赢家。

#1


2  

Even if it is slower, I doubt this would be the bottleneck in your system. You are very likely going to have more significant performance issues in other parts of your program.

即使速度较慢,我也怀疑这会是你系统的瓶颈。您很可能会在程序的其他部分出现更严重的性能问题。

Also, the answer to this question provides more details, but basically "It's platform dependent.". It's not necessarily true 64 bit will be slower than 32 bit.

此外,这个问题的答案提供了更多细节,但基本上“它依赖于平台。”。 64位将比32位慢,这不一定是真的。

#2


1  

This is likely to be platform dependant. I have seen cases where using long instead of int is about 10% faster. The 64-bit JVM for Java 5.0 was about 5% - 10% slower than the 32-bit JVM for Java 5.0. Java 6 doesn't appear to have this problem.

这很可能取决于平台。我见过使用long而不是int的情况快了大约10%。用于Java 5.0的64位JVM比用于Java 5.0的32位JVM慢约5%-10%。 Java 6似乎没有这个问题。

I imagine the cost of dividing by 10000 far outweighs the cost of using a long instead of an int value.

我认为除以10000的成本远远超过使用long而不是int值的成本。

You could also use double, rounding the result to four decimal places before printing/outputting it.

您也可以使用double,在打印/输出结果之前将结果四舍五入到四位小数。

#3


1  

Generally, the more data you need to hurl around, the slower it is, so even on a 64-bit VM sticking to int instead of long is faster in most cases.

一般来说,你需要投入的数据越多,它就越慢,因此即使在64位虚拟机上坚持使用int而不是long也会在大多数情况下更快。

This becomes very clear if you think in terms of memory footprint: an array of 1 million ints requires 4MB, 1M longs eat 8MB.

如果从内存占用方面考虑,这一点就变得非常清楚:一百万个整数的阵列需要4MB,1M长就需要8MB。

As for the computational speed, there is some overhead to perform operations on 64-bit types with 32-bit instructions. But even if the VM can use 64-bit instructions (which it should on a 64-bit VM), depending on the CPU they may still be slower than their 32-bit counterparts (add/subtract will probably go in one clock, but multiply and divide in 64-bit are usually slower than in 32-bit).

至于计算速度,使用32位指令对64位类型执行操作会有一些开销。但即使VM可以使用64位指令(它应该在64位VM上),取决于CPU,它们可能仍然比它们的32位速率慢(加/减可能会在一个时钟内,但是64位乘法和除法通常比32位慢。


A very common misconception is that integer math is faster than floating point math. As soon as you need to perform extra operations to "normalize" your integers, floating point will beat your integer implementation flat in performance. The actual differences in clock cycles spent between integer and floating point instructions is neglible for most applications, so if floating point is waht you need, use it and don't attempt to emulate it yourself.

一个非常常见的误解是整数数学比浮点数学更快。一旦你需要执行额外的操作来“归一化”你的整数,浮点数就会在性能上超过你的整数实现。对于大多数应用程序,在整数和浮点指令之间花费的时钟周期的实际差异是可以忽略的,因此如果您需要浮动点,请使用它并且不要尝试自己模拟它。

For the question which type to actually use: Use the type thats most appropriate in terms of data representation. Worry about performance when you get there. Look at wht operations you need to perform and what precision you need. Then select the type that offers exactly that. Judging by the libraries you mentioned, double will probably be the winner of that.

对于实际使用哪种类型的问题:使用最适合数据表示的类型。当你到达那里时担心性能。查看您需要执行的操作以及所需的精度。然后选择提供该类型的类型。从您提到的图书馆来看,双倍可能会成为赢家。