private static void FirstTest() { IEnumerable<Item> items = new List<Item>(Enumerable.Range(0, 1000).Select(a => new Item(a))); // sorting randomly items = items.OrderBy(x => new Guid()).ToArray(); int n = 100; Func<Item, bool> filter = x => x.Size == n; Run("First", () => items.First(filter)); Run("Where First", () => items.Where(filter).First()); filter = x => x.Size < n; Run("First", () => items.First(filter)); Run("Where First", () => items.Where(filter).First()); filter = x => x.Size > n; Run("First", () => items.First(filter)); Run("Where First", () => items.Where(filter).First()); } public class Item { public String Name { get; set; } public int Size { get; set; } public Item(int n) { Size = n; Name = "a" + Size; } } public static void Run(String title, Func<object> action) { Stopwatch watch = Stopwatch.StartNew(); for (int i = 0; i < 40000; i++) { action(); } watch.Stop(); Console.WriteLine("{0} milliseconds for {1}", watch.ElapsedMilliseconds, title); }
输出结果:
标签:Run,items,filter,Size,直接,Where,First From: https://www.cnblogs.com/huangkai00000/p/17346817.html