In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what is the difference between the methods of List search and sorting". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In .NET 1.1, I also had a lot of programmers like me who used ArrayList. At that time, if you want to find this collection element, you will mostly use the for loop, of course, you can also use the BinarySearch method. But since the advent of. NET 2.0 and .NET 3.5, ArrayList has been rarely used, and List is considered to be superior to ArrayList in performance. Now that you have List and LINQ, the query for the LIST collection is no longer single. I list three methods here: they do one thing together, looking for elements with numbers greater than 50000 in a collection of Person. The Person class is defined as follows:
Public class Person {public string firstName {get; set;} public string lastName {get; set;} public int ID {get; set;}}
First construct a generic collection of Person. Of course, there will not be such a large set in general, but it is necessary to compare the search performance of different methods.
List list = new List (); for (int I = 0; I
< 100001; i++) { Person p = new Person(); p.firstName = i.ToString() + "firstName"; p.lastName = i.ToString() + "lastName"; list.Add(p); } 1:List提供的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 persons = list.FindAll (new Predicate (fp.PersonPredicate)); sw.Stop (); Response.Write ("Find method search time" + sw.ElapsedMilliseconds.ToString () + "")
2: traditional for cycle.
Sw.Start (); List newPersons = new List (); for (int j = 0; j
< list.Count; j++) { if (list[j].ID >= 50000) {newPersons.Add (list [j]);}} sw.Stop (); Response.Write ("for Loop search time" + sw.ElapsedMilliseconds.ToString () + "")
Query in 3:LINQ mode.
Sw = new Stopwatch (); sw.Start (); var pn = (from m in list where m.ID > = 50000 select m). ToList (); sw.Stop (); Response.Write ("linq search time" + sw.ElapsedMilliseconds.ToString () + "")
Output: although it takes about the same time, it is still the traditional for loop performance, although there is nothing new in the writing. I think one good thing about FindAll is that if there are many query methods for List (of course, in fact, the Person class will not be so simple), it is better to enclose the query method in the FindPerson class, so it will be very easy to call the query externally. If it is in other ways, it can also be encapsulated, but obviously the code structure is slightly worse. Linq query, in terms of flexibility, I think it is worse than the first two.
Find method search time 5
For loop search time 4
Linq search time 6
Second: let's take a look at the sorting of List. Here we compare the Sort method provided by List with the orderby of Linq mode.
1:Sort . Here we first write a custom comparison class PersonComparer
Public class PersonComparer: IComparer {public int Compare (Person x, Person y) {return x.ID.CompareTo (y.ID);}}
Sort code:
Sw = new Stopwatch (); sw.Start (); list.Sort (new PersonComparer ()); sw.Stop (); Response.Write ("Sort sorting time" + sw.ElapsedMilliseconds.ToString () + "")
2:Linq mode.
Sw = new Stopwatch (); sw.Start (); var pn = (from m in list orderby m.ID descending select m). ToList (); sw.Stop (); Response.Write ("linq sorting time" + sw.ElapsedMilliseconds.ToString () + "")
Output result: linq still has a great advantage in sorting.
Sort sorting time 670
The time of linq sorting is 195
This is the end of the content of "what is the difference between List search and sorting methods". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.