Float vs Double Performance

后端 未结 4 637
鱼传尺愫
鱼传尺愫 2020-11-28 04:32

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 process

4条回答
  •  执笔经年
    2020-11-28 05:24

    It depends on 32-bit or 64-bit system. If you compile to 64-bit, double will be faster. Compiled to 32-bit on 64-bit (machine and OS) made float around 30% faster:

        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);
        }
    

提交回复
热议问题