
为什么80%的码农都做不了架构师?>>>
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 pd = new List();
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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/143617.html