大家好,又见面了,我是你们的朋友全栈君。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class People
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private Byte sex;
public Byte Sex
{
get { return sex; }
set { sex = value; }
}
private string school;
public string School
{
get { return school; }
set { school = value; }
}
}
class Program
{
static void Main(string[] args)
{
List<People> pd = new List<People>();
for(int i = 0; i < 72000; i++)
{
People people = new People();
people.Id = i;
people.Sex = 1;
if(i<36000){
people.Age = 23;
people.Name= "MJ" + i;
people.School="NC";
}else
{
people.Name= "KD" + i;
people.Age = 24;
people.School="UCLA";
}
pd.Add(people);
}
System.DateTime currentTime0 = System.DateTime.Now;
foreach (People p in pd)
{
p.Id += 1;
p.Name = "aaa";
p.Age = 40;
p.Sex = 1;
Console.WriteLine(p.Id + ", " + p.Name + ", " + p.School);
}
System.DateTime currentTime1 = System.DateTime.Now;
Parallel.ForEach(pd, p =>
{
p.Id += 1;
p.Name = "bbb";
p.Age = 35;
p.Sex =0;
Console.WriteLine(p.Id + ", " + p.Name + ", " + p.School);
});
System.DateTime currentTime2 = System.DateTime.Now;
Console.WriteLine(currentTime0.ToString() + " " + currentTime0.Millisecond.ToString());
Console.WriteLine(currentTime1.ToString() + " " + currentTime1.Millisecond.ToString());
Console.WriteLine(currentTime2.ToString() + " " + currentTime2.Millisecond.ToString());
Console.ReadKey();
}
}
}
输出过程中,明显看到第二个循环比第一个快了很多很多,但是看最后的时间差却失望了:
2014/08/29 15:37:50 401
2014/08/29 15:37:59 246
2014/08/29 15:38:08 185
转载于:https://my.oschina.net/mj23/blog/308043
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/161809.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...