using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
public class Test
{
class Point
{
public int X { get; set; }
public int Y { get; set; }
}
class Program
{
static void Main()
{
var random = new Random();
List<Point> points = Enumerable.Range(0, 10000000).Select(x => new Point { X = random.Next(1000), Y = random.Next(1000) }).ToList();
int conditionValue = 250;
Console.WriteLine($"Condition value: {conditionValue}");
Stopwatch sw = new Stopwatch();
sw.Start();
int hitCount1 = 0;
var points1 = points.Where(x =>
{
hitCount1++;
return x.X < conditionValue;
}).Select(x =>
{
hitCount1++;
return x.Y;
}).ToArray();
sw.Stop();
Console.WriteLine($"Where -> Select: {sw.ElapsedMilliseconds} ms, {hitCount1} hits");
sw.Restart();
int hitCount2 = 0;
var points2 = points.Select(x =>
{
hitCount2++;
return x.Y;
}).Where(x =>
{
hitCount2++;
return x < conditionValue;
}).ToArray();
sw.Stop();
Console.WriteLine($"Select -> Where: {sw.ElapsedMilliseconds} ms, {hitCount2} hits");
Console.ReadLine();
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uTGlucTsKdXNpbmcgU3lzdGVtLkRpYWdub3N0aWNzOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKCnB1YmxpYyBjbGFzcyBUZXN0CnsKCWNsYXNzIFBvaW50CnsKICAgIHB1YmxpYyBpbnQgWCB7IGdldDsgc2V0OyB9CiAgICBwdWJsaWMgaW50IFkgeyBnZXQ7IHNldDsgfQp9CgpjbGFzcyBQcm9ncmFtCnsKICAgIHN0YXRpYyB2b2lkIE1haW4oKQogICAgewogICAgICAgIHZhciByYW5kb20gPSBuZXcgUmFuZG9tKCk7CiAgICAgICAgTGlzdDxQb2ludD4gcG9pbnRzID0gRW51bWVyYWJsZS5SYW5nZSgwLCAxMDAwMDAwMCkuU2VsZWN0KHggPT4gbmV3IFBvaW50IHsgWCA9IHJhbmRvbS5OZXh0KDEwMDApLCBZID0gcmFuZG9tLk5leHQoMTAwMCkgfSkuVG9MaXN0KCk7CgogIAkJaW50IGNvbmRpdGlvblZhbHVlID0gMjUwOwoJCUNvbnNvbGUuV3JpdGVMaW5lKCQiQ29uZGl0aW9uIHZhbHVlOiB7Y29uZGl0aW9uVmFsdWV9Iik7CgkJCiAgICAgICAgU3RvcHdhdGNoIHN3ID0gbmV3IFN0b3B3YXRjaCgpOwogICAgICAgIHN3LlN0YXJ0KCk7CgogICAgICAgIGludCBoaXRDb3VudDEgPSAwOwogICAgICAgIHZhciBwb2ludHMxID0gcG9pbnRzLldoZXJlKHggPT4KICAgICAgICB7CiAgICAgICAgICAgIGhpdENvdW50MSsrOwogICAgICAgICAgICByZXR1cm4geC5YIDwgY29uZGl0aW9uVmFsdWU7CiAgICAgICAgfSkuU2VsZWN0KHggPT4KICAgICAgICB7CiAgICAgICAgICAgIGhpdENvdW50MSsrOwogICAgICAgICAgICByZXR1cm4geC5ZOwogICAgICAgIH0pLlRvQXJyYXkoKTsKCiAgICAgICAgc3cuU3RvcCgpOwogICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCQiV2hlcmUgLT4gU2VsZWN0OiB7c3cuRWxhcHNlZE1pbGxpc2Vjb25kc30gbXMsIHtoaXRDb3VudDF9IGhpdHMiKTsKCiAgICAgICAgc3cuUmVzdGFydCgpOwoKICAgICAgICBpbnQgaGl0Q291bnQyID0gMDsKICAgICAgICB2YXIgcG9pbnRzMiA9IHBvaW50cy5TZWxlY3QoeCA9PgogICAgICAgIHsKICAgICAgICAgICAgaGl0Q291bnQyKys7CiAgICAgICAgICAgIHJldHVybiB4Llk7CiAgICAgICAgfSkuV2hlcmUoeCA9PgogICAgICAgIHsKICAgICAgICAgICAgaGl0Q291bnQyKys7CiAgICAgICAgICAgIHJldHVybiB4IDwgY29uZGl0aW9uVmFsdWU7CiAgICAgICAgfSkuVG9BcnJheSgpOwoKICAgICAgICBzdy5TdG9wKCk7CiAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoJCJTZWxlY3QgLT4gV2hlcmU6IHtzdy5FbGFwc2VkTWlsbGlzZWNvbmRzfSBtcywge2hpdENvdW50Mn0gaGl0cyIpOwoKICAgICAgICBDb25zb2xlLlJlYWRMaW5lKCk7CiAgICB9Cn0KfQ==