作者DearKurt ("小朋友"会自己找出路...)
看板C_Sharp
标题Re: [心得] 比较阵列的相减後取绝对值速度
时间Sat Jan 16 21:40:25 2010
小弟只针对一维的程式去修改成多执行绪
static void Main(string[] args)
{
ushort[] myArray1 = new ushort[2452 * 2054];
ushort[] myArray2 = new ushort[2452 * 2054];
ushort[] myArray3 = new ushort[2452 * 2054];
//视核心数量 双核就2个AutoResetEvent
AutoResetEvent[] aREs = new AutoResetEvent[2];
Random myRnd = new Random();
TimeSpan clock01 = new TimeSpan();
int cycleTimes = 10;
for (int i = 0; i < cycleTimes; i++)
{
//AutoResetEvent初始化
for (int j = 0; j < aREs.Length; j++)
{
aREs[j] = new AutoResetEvent(false);
}
for (int j = 0; j < myArray1.Length; j++)
{
//NextDouble()取整数後是0 改成这样可能比较适当
myArray1[j] = (ushort)myRnd.Next(256*256);
myArray2[j] = (ushort)myRnd.Next(256*256);
}
System.DateTime startTime = DateTime.Now;
//把原本for loop拆成多个执行绪去执行
for (int k = 0; k < aREs.Length; k++)
{
int y = k;
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object o)
{
for (int j = y; j < myArray1.Length; j += aREs.Length)
{
myArray3[j] = (ushort)Math.Abs(myArray1[j] - myArray2[j]);
}
aREs[y].Set();
}));
}
WaitHandle.WaitAll(aREs);
clock01 = (DateTime.Now - startTime) + clock01;
}
Console.WriteLine((clock01.TotalMilliseconds / cycleTimes).ToString());
Console.ReadLine();
}
cpu是AthlonIIx2 240超到3.5G
用build完的执行档跑
单执行绪平均45ms 双执行绪平均26ms
如果是四核心的i7 每秒超过30张应该是没问题了XD
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 124.11.129.161
※ 编辑: DearKurt 来自: 124.11.129.161 (01/17 00:56)