在 C# 中,double 比浮点数快吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/158889/
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-03 16:08:41  来源:igfitidea点击:

Are doubles faster than floats in C#?

提问by Trap

I'm writing an application which reads large arrays of floats and performs some simple operations with them. I'm using floats, because I thought it'd be faster than doubles, but after doing some research I've found that there's some confusion about this topic. Can anyone elaborate on this?

我正在编写一个应用程序,它读取大量的浮点数并用它们执行一些简单的操作。我正在使用浮动,因为我认为它比双打要快,但在做了一些研究之后,我发现这个话题有些混乱。任何人都可以详细说明这一点吗?

采纳答案by user7116

The short answer is, "use whichever precision is required for acceptable results."

简短的回答是,“使用可接受的结果所需的任何精度。”

Your one guarantee is that operations performed on floating point data are done in at least the highest precision member of the expression. So multiplying two float's is done with at least the precision of float, and multiplying a floatand a doublewould be done with at least double precision. The standard states that "[floating-point] operations may be performed with higher precision than the result type of the operation."

您的一个保证是对浮点数据执行的操作至少在表达式的最高精度成员中完成。因此,将两个float相乘至少以float的精度完成,并且将一个float和一个double相乘至少以双精度完成。该标准规定“[浮点] 运算的执行精度可能高于运算的结果类型。”

Given that the JIT for .NET attempts to leave your floating point operations in the precision requested, we can take a look at documentation from Intel for speeding up our operations. On the Intel platform your floating point operations may be done in an intermediate precision of 80 bits, and converted down to the precision requested.

鉴于 .NET 的 JIT 试图让您的浮点运算保持所要求的精度,我们可以查看 Intel 的文档以加快我们的运算速度。在 Intel 平台上,您的浮点运算可能以 80 位的中间精度完成,并转换为所需的精度。

From Intel's guide to C++ Floating-point Operations1(sorry only have dead tree), they mention:

从英特尔的 C++ 浮点运算1 指南(抱歉只有死树),他们提到:

  • Use a single precision type (for example, float) unless the extra precision obtained through double or long double is required. Greater precision types increase memory size and bandwidth requirements. ...
  • Avoid mixed data type arithmetic expressions
  • 除非需要通过 double 或 long double 获得的额外精度,否则请使用单精度类型(例如,float)。更高的精度类型会增加内存大小和带宽要求。...
  • 避免混合数据类型的算术表达式

That last point is important as you can slow yourself down with unnecessary casts to/from float and double, which result in JIT'd code which requests the x87 to cast away from its 80-bit intermediate format in between operations!

最后一点很重要,因为您可以通过不必要的到/从 float 和 double 的强制转换来减慢自己的速度,这会导致 JIT 代码要求 x87 在两次操作之间放弃其 80 位中间格式!

1. Yes, it says C++, but the C# standard plus knowledge of the CLR lets us know the information for C++ should be applicable in this instance.

1. 是的,它说的是 C++,但是 C# 标准加上 CLR 的知识让我们知道 C++ 的信息应该适用于这种情况。

回答by torial

This indicates that floats are slightly faster than doubles: http://www.herongyang.com/cs_b/performance.html

这表明浮动比双打略快:http: //www.herongyang.com/cs_b/performance.html

In general, any time you do a comparison on performance, you should take into account any special cases, like does using one type require additional conversions or data massaging? Those add up and can belie generic benchmarks like this.

一般来说,任何时候进行性能比较时,都应该考虑任何特殊情况,比如使用一种类型是否需要额外的转换或数据按摩?这些加起来可以掩盖这样的通用基准。

回答by Steven A. Lowe

Floats should be faster on a 32-bit system, but profile the code to make sure you're optimizing the right thing.

浮点数在 32 位系统上应该更快,但要分析代码以确保优化正确的东西。

回答by Mark Bessey

If load & store operations are the bottleneck, then floats will be faster, because they're smaller. If you're doing a significant number of calculations between loads and stores, it should be about equal.

如果加载和存储操作是瓶颈,那么浮动会更快,因为它们更小。如果您在加载和存储之间进行大量计算,则应该大致相等。

Someone else mentioned avoiding conversions between float & double, and calculations that use operands of both types. That's good advice, and if you use any math library functions that return doubles (for example), then keeping everything as doubles will be faster.

其他人提到避免在 float 和 double 之间进行转换,以及使用这两种类型的操作数的计算。这是一个很好的建议,如果您使用任何返回双精度的数学库函数(例如),那么将所有内容保持为双精度会更快。

回答by Dave Tarkowski

I profiled a similar question a few weeks ago. The bottom line is that for x86 hardware, there is no significant difference in the performance of floats versus doubles unless you become memory bound, or you start running into cache issue. In that case floats will generally have the advantage because they are smaller.

几周前我描述了一个类似的问题。最重要的是,对于 x86 硬件,除非您受到内存限制,或者您开始​​遇到缓存问题,否则浮点数与双精度数的性能没有显着差异。在这种情况下,浮点数通常具有优势,因为它们更小。

Current Intel CPUs perform all floating point operations in 80 bit wide registers so the actual speed of the computation shouldn't vary between floats and doubles.

当前的 Intel CPU 在 80 位宽的寄存器中执行所有浮点运算,因此计算的实际速度不应在浮点数和双精度数之间变化。

回答by Dark Shikari

With 387 FPU arithmetic, float is only faster than double for certain long iterative operations like pow, log, etc (and only if the compiler sets the FPU control word appropriately).

使用 387 FPU 算法时,对于某些长迭代操作(如 pow、log 等)(并且仅当编译器适当设置 FPU 控制字时),float 仅比 double 快。

With packed SSE arithmetic, it makes a big difference though.

使用打包的 SSE 算法,它会产生很大的不同。

回答by Dark Shikari

I'm writing a ray tracer, and replacing the floats with doubles for my Color class gives me a 5% speedup. Replacing the Vectors floats with doubles is another 5% faster! Pretty cool :)

我正在编写一个光线追踪器,并用我的 Color 类的双打替换浮点数使我的速度提高了 5%。用双打替换 Vectors 浮点数又快了 5%!很酷:)

That's with a Core i7 920

那是 Core i7 920

回答by Tomas Pajonk

I have always thought that the processors were optimized or the same regardless of float or double. Searching for optimizations on my intensive computations (lots of gets from a matrix, comparisons of two values) I found out that floats run about 13% faster.

我一直认为处理器经过优化或相同,无论是浮点数还是双精度数。在我的密集计算中搜索优化(从矩阵中获取大量数据,两个值的比较),我发现浮点数的运行速度提高了大约 13%。

This surprised me, but I guess it is due to the nature of my problem. I don't do casts between float and double in the core of the operations, and my computations are mainly adding, multiplying and subtracting.

这让我感到惊讶,但我想这是由于我的问题的性质。我在运算的核心中不做 float 和 double 之间的转换,我的计算主要是加法、乘法和减法。

This is on my i7 920, running a 64-bit operating system.

这是在我的 i7 920 上,运行 64 位操作系统。

回答by Matthijs Wessels

I just read the "Microsoft .NET Framework-Application Development Foundation 2nd" for the MCTS exam 70-536 and there is a note on page 4 (chapter 1):

我刚刚阅读了 MCTS 考试 70-536 的“Microsoft .NET Framework-Application Development Foundation 2nd”,并且在第 4 页(第 1 章)上有一个注释:

NOTE Optimizing performance with built-in types
The runtime optimizes the performance of 32-bit integer types (Int32 and UInt32), so use those types for counters and other frequently accessed integral variables. For floating-point operations, Double is the most efficient type because those operations are optimized by hardware.

注意 使用内置类型
优化性能 运行时优化了 32 位整数类型(Int32 和 UInt32)的性能,因此将这些类型用于计数器和其他经常访问的整数变量。对于浮点运算,Double 是最有效的类型,因为这些运算是由硬件优化的。

It's written by Tony Northrup. I don't know if he's an authority or not, but I would expect that the official book for the .NET exam should carry some weight. It is of course not a gaurantee. I just thought I'd add it to this discussion.

它是由托尼·诺斯鲁普 (Tony Northrup) 编写的。我不知道他是否是权威,但我希望 .NET 考试的官方书籍应该有一定的分量。这当然不是保证。我只是想我会把它添加到这个讨论中。

回答by CReeK

Matthijs,

马蒂斯,

You are wrong. 32-bit is far more efficient than 16-bit - in modern processors... Perhaps not memory-wise, but in effectiveness 32-bit is the way to go.

你错了。32 位远比 16 位高效 - 在现代处理器中......也许不是内存方面,但在有效性方面 32 位是要走的路。

You really should update your professor to something more "up-to-date". ;)

你真的应该让你的教授更新一些更“最新”的东西。;)

Anyway, to answer the question; float and double has exactly the same performance, at least on my Intel i7 870 (as in theory).

无论如何,回答问题;float 和 double 具有完全相同的性能,至少在我的 Intel i7 870 上(理论上如此)。

Here are my measurements:

以下是我的测量结果:

(I made an "algorithm" that I repeated for 10,000,000 times, and then repeated that for 300 times, and out of that I made a average.)

(我做了一个“算法”,我重复了 10,000,000 次,然后重复了 300 次,然后我做了一个平均值。)

double
-----------------------------
1 core  = 990 ms
4 cores = 340 ms
6 cores = 282 ms
8 cores = 250 ms

float
-----------------------------
1 core  = 992 ms
4 cores = 340 ms
6 cores = 282 ms
8 cores = 250 ms