在什么情况下最好在 Java 中使用浮点数而不是双精度数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21445662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:46:04  来源:igfitidea点击:

In what situations is it better to use a float over a double in Java?

javaperformancedouble

提问by Julian

More specifically, how extreme is the performance gain when using a float instead of a double in Java?

更具体地说,在 Java 中使用浮点数而不是双精度数时的性能提升有多极端?

采纳答案by nico_c

Since your question is mostly about performance, this article presents you with some specific calculations (keep in mind though that this article is specific to neural networks, and your calculations may be completely different to what they're doing in the article): http://web.archive.org/web/20150310213841/http://www.heatonresearch.com/content/choosing-between-java%E2%80%99s-float-and-double

由于您的问题主要是关于性能的,因此本文为您提供了一些具体的计算(请记住,尽管本文是针对神经网络的,但您的计算可能与他们在文章中所做的完全不同):http: //web.archive.org/web/20150310213841/http://www.heatonresearch.com/content/choosing-between-java%E2%80%99s-float-and-double

Some of the relevant material from the link is reproduced here:

链接中的一些相关材料转载于此处:

Both double and float can support relatively large numbers. The upper and lower range are really not a consideration for neural networks. Float can handle numbers between 1.40129846432481707e-45 to 3.40282346638528860e+38...Basically, float can handle about 7 decimal places. A double can handle about 16 decimal places.

Matrix multiplication is one of the most common mathematical operations for neural network programming. By no means is it the only operation, but it will provide a good benchmark. The following program will be used to benchmark a double.

double 和 float 都可以支持相对较大的数字。上下范围真的不是神经网络的考虑因素。Float 可以处理 1.40129846432481707e-45 到 3.40282346638528860e+38 之间的数字......基本上,float 可以处理大约 7 个小数位。double 可以处理大约 16 位小数。

矩阵乘法是神经网络编程中最常见的数学运算之一。这绝不是唯一的操作,但它将提供一个很好的基准。以下程序将用于对双精度进行基准测试。

Skipping all the code, the table on the website shows that for a 100x100 matrix multiplication, they have a gain in performance of around 10% if they use doubles. For a 500x100 matrix multiplication, the performance loss because of using doubles is around 7%. And for a 1000x1000 matrix multiplication, that loss is around 17%.

跳过所有代码,网站上的表格显示,对于 100x100 矩阵乘法,如果使用双精度数,它们的性能提高了 10% 左右。对于 500x100 矩阵乘法,由于使用双精度导致的性能损失约为 7%。对于 1000x1000 矩阵乘法,损失约为 17%。

For the small 100x100 matrix switching to float may actually decrease performance. As the size of the matrix increases, the percent gain increases. With a very large matrix the performance gain increases to 17%. 17% is worth considering.

对于小型 100x100 矩阵切换到浮动实际上可能会降低性能。随着矩阵大小的增加,百分比增益增加。使用非常大的矩阵,性能增益增加到 17%。17% 值得考虑。

回答by mikera

Normally, I would use a double, because floatdoesn't have sufficient accuracy for many numerical use cases, and the performance difference is small enough not to matter.

通常,我会使用double, 因为float对于许多数值用例没有足够的准确性,并且性能差异小到无关紧要。

As always, performance is implementation dependent so you will need to benchmark on your particular case in order to determine if it "matters" or not.

与往常一样,性能取决于实现,因此您需要对特定情况进行基准测试,以确定它是否“重要”。

In general I have found:

一般来说,我发现:

  • The performance difference for individual operations is pretty small, especially on 64-bit machines. Both a floatand a longwill fit in a 64-bit machine word. Often there is zero difference.
  • floats have a slight advantage in that they consume less memory, and this can help with reducing CPU cache pressure. I've found floats to be 30-50% faster when doing simple operations over large arrays.
  • 单个操作的性能差异非常小,尤其是在 64 位机器上。afloat和 along都适合 64 位机器字。通常是零差异。
  • floats 有一点优势,因为它们消耗更少的内存,这有助于降低 CPU 缓存压力。我发现在对大型数组进行简单操作时,浮点数要快 30-50%。

回答by Patricia Shanahan

There are two conditions that should be met to use float instead of double:

使用 float 而不是 double 应满足两个条件:

  1. There is a significant, useful performance gain.
  2. Float is precise enough for the calculation in question.
  1. 有一个显着的、有用的性能增益。
  2. Float 对于所讨论的计算来说足够精确。

Evaluating the first condition is relatively simple and easy. Measure the job using double. Measure again, with the same inputs and other conditions, using float. Is the float version significantly faster? Generally, the main benefit comes from more efficient use of caches, memory, and data transfer bandwidth.

评估第一个条件相对简单易行。使用 double 测量作业。再次测量,使用相同的输入和其他条件,使用浮点数。浮动版本明显更快吗?通常,主要好处来自更有效地使用缓存、内存和数据传输带宽。

The difficult part is evaluating the second condition. Getting insufficiently precise answers, or even wrong answers, is useless. For many calculations, relatively simple analysis can show double is precise enough. Showing the same for float is often much harder.

困难的部分是评估第二个条件。得到不够精确的答案,甚至是错误的答案,都是没有用的。对于许多计算,相对简单的分析可以表明 double 已经足够精确。为浮动显示相同的内容通常要困难得多。

回答by Murillo Ferreira

Why intinstead of long?

为什么int而不是long

Why shortinstead of int?

为什么short而不是int

Why byteinstead of short?

为什么byte而不是short

This is all about performance and memory use reasons. In theory, more range means more memory, more memory needs more from your hardware and it means that more battery will be used.

这完全是关于性能和内存使用的原因。从理论上讲,更大的范围意味着更多的内存,更多的内存需要更多的硬件,这意味着将使用更多的电池。

Today's machines have high performance with primitive types. So, after all, you may not see any difference if you use long instead of double and vice versa, but, Internet Connections can be slow depending of the country, so it is best use the float instead of double in this case.

今天的机器在原始类型方面具有很高的性能。因此,毕竟,如果您使用 long 而不是 double,您可能看不到任何区别,反之亦然,但是,Internet 连接可能会因国家/地区而变慢,因此在这种情况下最好使用 float 而不是 double。

Lets talk about shortvs int.

让我们谈谈shortvs int

Short use less memory than int, but it takes 30% more time to be created (Java), so would be better aways use intinstead of short? Depends, an array with 100 shorts will use a much less memory than an array of 100 ints.

短期使用较少的内存比int,但它需要被创建(Java)的30%以上的时间,所以会更好赠品使用int的不是short?视情况而定,100short秒的数组将使用比 100int秒的数组少得多的内存。

A loop with shortcan be more more efficient than a loop with int.

循环 withshort可能比循环更有效int

On internet, shortis much more efficient than int, doens't matter if it takes 30% more to be created, the bandwith will be small and the experience will be better.

在互联网上,short比 效率高得多int,创建多 30% 没关系,带宽会小,体验会更好。

I'd sugest you to use only what you'll need. Remember: why 80 if 8 can do it?

我建议你只使用你需要的东西。记住:如果 8 可以做到,为什么 80?

回答by AlexWien

Mostly at Embedded Systems without a CPU having fast double operations. Eg navigation systems, with cpu intensive calculations. But in java many Math libs implicitly convert to double, so in many cases it makes little sense to use float.

主要是在没有 CPU 的嵌入式系统中进行快速双重运算。例如,具有 CPU 密集型计算的导航系统。但是在 Java 中,许多 Math 库都隐式转换为 double,因此在许多情况下使用 float 毫无意义。

floats need half the memory, and advantage if you have very huge arrays and limited memory (in embedded devices)

浮点数需要一半的内存,如果你有非常大的数组和有限的内存(在嵌入式设备中)