代码 List<Person> list = new List<Person>(); for (int i = 0; i < 100001; i++) { Person p = new Person(); p.firstName = i.ToString() + "firstName"; p.lastName = i.ToString() + "lastName"; p.ID = i; list.Add(p);
}
1:List<T>提供的FindAll方式。
代码 public class FindPerson { public string firstName; public FindPerson(string _firstName) { this.firstName = _firstName; } public bool PersonPRedicate(Person p) { return p.ID >= 50000; } } Stopwatch sw = new Stopwatch(); sw.Start(); List<Person> persons = list.FindAll(new Predicate<Person>(fp.PersonPredicate)); sw.Stop(); Response.Write("Find方法搜索用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");
2:传统的for循环。
代码 sw.Start(); List<Person> newPersons = new List<Person>(); for (int j = 0; j < list.Count; j++) { if (list[j].ID >= 50000) { newPersons.Add(list[j]);
代码 sw = new Stopwatch(); sw.Start(); var pn = (from m in list where m.ID >=50000 select m).ToList <Person >(); sw.Stop(); Response.Write("linq搜索用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");
代码 sw = new Stopwatch(); sw.Start(); var pn = (from m in list orderby m.ID descending select m).ToList<Person>(); sw.Stop(); Response.Write("linq排序用时" + sw.ElapsedMilliseconds.ToString() + "<br/>");