1: void Main()
2: {
3: var stopWatch = new Stopwatch();
4: const int max = 100000000;
5: List<int> list = new List<int>();
6:
7: //insert max amount of numbers into collection
8: var rnd = new Random(max/10000);
9: int rn = 0;
10: for (int i = 1; i < max; i++)
11: {
12: rn = rnd.Next(i);
13: list.Add(rn);
14: }
15:
16: Console.WriteLine("Where(a => a == rn).FirstOrDefault()");
17: stopWatch.Start();
18: int r1 = list.Where(a => a == rn).FirstOrDefault();
19: Console.WriteLine("{0} ticks - {1} ms", stopWatch.ElapsedTicks, stopWatch.ElapsedMilliseconds);
20:
21: Console.WriteLine();
22: Console.WriteLine("FirstOrDefault(a => a == rn)");
23: stopWatch.Restart();
24: int r2 = list.FirstOrDefault(a => a == rn);
25: Console.WriteLine("{0} ticks - {1} ms", stopWatch.ElapsedTicks, stopWatch.ElapsedMilliseconds);
26:
27: stopWatch.Stop();
28: }