简体   繁体   English

浮动与双重表现

[英]Float vs Double Performance

I did some timing tests and also read some articles like this one (last comment), and it looks like in Release build, float and double values take the same amount of processing time. 我做了一些时间测试,也喜欢读一些文章, 这样一个 (最后的评论),它看起来像在发布版本,float和double值采取的处理时间相同。

How is this possible? 这怎么可能? When float is less precise and smaller compared to double values, how can the CLR get doubles into the same processing time? 当float值与double值相比精度更低且更小时,CLR如何在相同的处理时间内获得双倍的效果?

On x86 processors, at least, float and double will each be converted to a 10-byte real by the FPU for processing. 在x86处理器上,至少floatdouble float将由FPU转换为10字节实数进行处理。 The FPU doesn't have separate processing units for the different floating-point types it supports. FPU没有针对它支持的不同浮点类型的单独处理单元。

The age-old advice that float is faster than double applied 100 years ago when most CPUs didn't have built-in FPUs (and few people had separate FPU chips), so most floating-point manipulation was done in software. 这种古老的建议表明, float速度比100年前大多数CPU没有内置FPU(并且很少有人拥有单独的FPU芯片)的速度要快double ,所以大多数浮点操作都是用软件完成的。 On these machines (which were powered by steam generated by the lava pits), it was faster to use float s. 在这些机器上(这是由通过熔岩凹坑产生的蒸汽驱动的),这更快地使用float秒。 Now the only real benefit to float s is that they take up less space (which only matters if you have millions of them). 现在float s的唯一真正好处是它们占用的空间更少(这只有你拥有数百万的空间才有意义)。

I had a small project where I used CUDA and I can remember that float was faster than double there, too. 我有一个小项目,我使用CUDA,我记得浮动速度比那里快一倍。 For once the traffic between Host and Device is lower (Host is the CPU and the "normal" RAM and Device is the GPU and the corresponding RAM there). 一旦主机和设备之间的流量较低(主机是CPU和“正常”RAM,设备是GPU和相应的RAM)。 But even if the data resides on the Device all the time it's slower. 但即使数据一直驻留在设备上,它也会变慢。 I think I read somewhere that this has changed recently or is supposed to change with the next generation, but I'm not sure. 我想我在某个地方看到这个已经改变了,或者应该改变下一代,但我不确定。

So it seems that the GPU simply can't handle double precision natively in those cases, which would also explain why GLFloat is usually used rather than GLDouble. 所以看起来GPU在这些情况下根本无法原生地处理双精度,这也解释了为什么通常使用GLFloat而不是GLDouble。

(As I said it's only as far as I can remember, just stumbled upon this while searching for float vs. double on a CPU.) (正如我所说的那样,只有我记得,只是在CPU上寻找浮动与双重时偶然发现了这一点。)

It depends on 32-bit or 64-bit system. 它取决于32位64位系统。 If you compile to 64-bit, double will be faster. 如果编译为64位,则double会更快。 Compiled to 32-bit on 64-bit (machine and OS) made float around 30% faster: 在64位(机器和操作系统)上编译为32位,浮动速度提高了约30%:

    public static void doubleTest(int loop)
    {
        Console.Write("double: ");
        for (int i = 0; i < loop; i++)
        {
            double a = 1000, b = 45, c = 12000, d = 2, e = 7, f = 1024;
            a = Math.Sin(a);
            b = Math.Asin(b);
            c = Math.Sqrt(c);
            d = d + d - d + d;
            e = e * e + e * e;
            f = f / f / f / f / f;
        }
    }

    public static void floatTest(int loop)
    {
        Console.Write("float: ");
        for (int i = 0; i < loop; i++)
        {
            float a = 1000, b = 45, c = 12000, d = 2, e = 7, f = 1024;
            a = (float) Math.Sin(a);
            b = (float) Math.Asin(b);
            c = (float) Math.Sqrt(c);
            d = d + d - d + d;
            e = e * e + e * e;
            f = f / f / f / f / f;
        }
    }

    static void Main(string[] args)
    {
        DateTime time = DateTime.Now;
        doubleTest(5 * 1000000);
        Console.WriteLine("milliseconds: " + (DateTime.Now - time).TotalMilliseconds);

        time = DateTime.Now;
        floatTest(5 * 1000000);
        Console.WriteLine("milliseconds: " + (DateTime.Now - time).TotalMilliseconds);

        Thread.Sleep(5000);
    }

仍然有一些情况下浮点数是首选的 - 例如,使用OpenGL编码,使用GLFloat数据类型(通常直接映射到16位浮点数)更常见,因为它在大多数GPU上比GLDouble更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM