7,660
社区成员




class Program
{
static void Main(string[] args)
{
int[] a = new int[100000000];
for (int i = 0; i < a.Length; i++)
{
a[i] = i;
}
Program pro = new Program();
pro.CommonLoop(a);//普通循环
GC.Collect();
pro.ConcurrencyLoop(a);//4.0新特性并行循环
Console.ReadLine();
}
private void CommonLoop(int[] a)
{
int[] b = new int[100000000];
Stopwatch newstopWatch = new Stopwatch();
newstopWatch.Start();
for (int i = 0; i < a.Length; i++)
{
b[i] = a[i] + 1;
}
newstopWatch.Stop();
Console.WriteLine("普通for循环执行时间:" + newstopWatch.ElapsedTicks);
}
private void ConcurrencyLoop(int[] a)
{
int[] b = new int[100000000];
Stopwatch newstopWatch = new Stopwatch();
newstopWatch.Start();
Parallel.For(0, a.Length, i =>
{
b[i] = a[i] + 1;
});
newstopWatch.Stop();
Console.WriteLine("并行循环执行时间:" + newstopWatch.ElapsedTicks);
}
}