日期:2014-05-20 浏览次数:21027 次
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);
//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
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);
}
}
}