C# 符点数取最近的整数哪个最快

 

方法一、使用Round

System.Diagnostics.Stopwatch MyWatch = new System.Diagnostics.Stopwatch();

            MyWatch.Start();

            long xint = 0;

            for (int i = 0; i < 1000000; i++)

            {

                xint = (Int64)(Math.Round(1000.9999111 * 1000, 0));

               

 

            }

           

            MyWatch.Stop();

            MessageBox.Show("插入数据:" + MyWatch.ElapsedMilliseconds.ToString() + "毫秒");

方法二、加0.5取整

private void button3_Click_3(object sender, EventArgs e)

        {

            System.Diagnostics.Stopwatch MyWatch = new System.Diagnostics.Stopwatch();

            MyWatch.Start();

            long xint = 0;

            for (int i = 0; i < 1000000; i++)

            {

                xint = (Int64)(1000.9999111 * 1000+0.5);

               

 

            }

          

            MyWatch.Stop();

           

            MessageBox.Show("插入数据:" + MyWatch.ElapsedMilliseconds.ToString() + "毫秒");

        }

测试:方法一为54ms,方法二为4ms,显然方法二快的多

 

你可能感兴趣的:(C# 符点数取最近的整数哪个最快)