日期:2014-05-20  浏览次数:20798 次

大家来看看运算符的效率,如何快速求uint抹去低16位之后的值
C# code

            long l1 = DateTime.Now.Ticks;
            for (int i = 0; i < 100000000; i++)
            {
                uint x = 0xffffaaaa & 0xffff0000;
            }
            TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - l1);

            long l2 = DateTime.Now.Ticks;

            for (int i = 0; i < 100000000; i++)
            {
                uint x = (0xffffaaaa >> 16 << 16);
            }
            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks - l2);
            long l3 = DateTime.Now.Ticks;
            unchecked
            {
                for (int i = 0; i < 100000000; i++)
                {
                    uint x = (0xffffaaaa - (ushort)(0xffffffff));
                }
            }
            TimeSpan ts3 = new TimeSpan(DateTime.Now.Ticks - l3);
            long l4 = DateTime.Now.Ticks;
            unsafe
            {
                uint test = 0xffffaaaa;
                for (int i = 0; i < 100000000; i++)
                {
                    //uint x = ((uint)(*(((ushort*)(&(test)))+1))) << 16 ;
                    uint x = ((uint)(*(((ushort*)(&(test)))+1))) * 65535 ;
                }
            }
            TimeSpan ts4 = new TimeSpan(DateTime.Now.Ticks - l4);
            long l5 = DateTime.Now.Ticks;
            unsafe
            {
                uint test = 0xffffaaaa;
                for (int i = 0; i < 100000000; i++)
                {
                    *((ushort*)(&(test))) = 0;
                    uint x = test;
                }
            }
            TimeSpan ts5 = new TimeSpan(DateTime.Now.Ticks - l5);

            Console.WriteLine("1:" + ts.Ticks + "\r\n2:" + ts2.Ticks + "\r\n3:" + ts3.Ticks + "\r\n4:" + ts4.Ticks + "\r\n5:" + ts5.Ticks);




代码贴上. 几种可以求uint抹去低16位之后的值的方法. 大家有更高效率的话可以补充.

大家先别去试,先猜想一下应该哪个最快,然后再去试.



------解决方案--------------------
整数加减最快
整数移位、位运算其次
整数乘除再其次

(2)最快。
------解决方案--------------------
来一个并行加快的:

C# code

            //using System.Threading.Tasks;
            Parallel.For(0, 9, pos =>
            {
                for (int i = 0; i < 10000000; i++)
                {
                    uint x = (0xffffaaaa >> 16 << 16);
                }
            });

------解决方案--------------------
。。。跑了下123一样快。。。

但是并行显然更快:

1:2656250
2:2656250
3:2656250
4:3125000
5:7187500
6:1406250

C# code
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            long l1 = DateTime.Now.Ticks;
            for (int i = 0; i < 100000000; i++)
            {
                uint x = 0xffffaaaa & 0xffff0000;
            }
            TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - l1);

            long l2 = DateTime.Now.Ticks;

            for (int i = 0; i < 100000000; i++)
            {
                uint x = (0xffffaaaa >> 16 << 16);
            }
            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks - l2);
            long l3 = DateTime.Now.Ticks;
            unchecked
            {
                for (int i = 0; i < 100000000; i++)
                {
                    uint x = (0xffffaaaa - (ushort)(0xffffffff));
                }
            }
            TimeSpan ts3 = new TimeSpan(DateTime.Now.Ticks - l3);
            long l4 = DateTime.Now.Ticks;
            unsafe
            {
                uint test = 0xffffaaaa;
                for (int i = 0; i < 100000000; i++)
                {
                    //uint x = ((uint)(*(((ushort*)(&(test)))+1))) << 16 ;
                    uint x = ((uint)(*(((ushort*)(&(test))) + 1))) * 65535;
                }
            }
            TimeSpan ts4 = new TimeSpan(DateTime.Now.Ticks - l4);
            long l5 = DateTime.Now.Ticks;
            unsafe
            {
                uint test = 0xffffaaaa;
                for (int i = 0; i < 100000000; i++)
                {
                    *((ushort*)(&(test))) = 0;
                    uint x = test;
                }
            }
            TimeSpan ts5 = new TimeSpan(DateTime.Now.Ticks - l5);
            long l6 = DateTime.Now.Ticks;
            Parallel.For(0, 9, pos =>
            {
                for (int i = 0; i < 10000000; i++)
                {
                    uint x = (0xffffaaaa >> 16 << 16);
                }
            });
            TimeSpan ts6 = new TimeSpan(DateTime.Now.Ticks - l6);

            Console.WriteLine("1:" + ts.Ticks + "\r\n2:" + ts2.Ticks + "\r\n3:" + ts3.Ticks + "\r\n4:" + ts4.Ticks + "\r\n5:" + ts5.Ticks + "\r\n6:" + ts6.Ticks);

        }
    }
}