255条数据
static void Main()
{
List<IPEndPoint> list = new List<IPEndPoint>();
for (int i =0;i<=255;i++)
{
int j = 34 + i / 255;
string ip = "188.10." + j + "." + i % 255;
int port = 26666;
IPAddress addr = IPAddress.Parse(ip);
IPEndPoint host = new IPEndPoint(addr, port);
list.Add(host);
}
System.Diagnostics.Stopwatch stop = new System.Diagnostics.Stopwatch();
stop.Start();
List<IPAddress> ips = new List<IPAddress>();
foreach (var item in list)
{
if (item.Address.ToString()=="188.10.34.140")
{
ips.Add(item.Address);
}
}
stop.Stop();
double mili = stop.Elapsed.TotalMilliseconds;
Console.WriteLine("loop: "+mili);
Console.WriteLine("loopCount: "+ips.Count);
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
List<IPAddress> lists = list.Where(e => e.Address.ToString() == "188.10.34.140").Select(e => e.Address).ToList<IPAddress>();
watch.Stop();
double milis = watch.Elapsed.TotalMilliseconds;
Console.WriteLine("lambda: "+milis);
Console.WriteLine("lambdaCount: "+lists.Count);
}
结果
loop: 0.0951
loopCount: 1
lambda: 0.5711
lambdaCount: 1
2550条
···
static void Main()
{
List
for (int i =0;i<=2550;i++)
{
int j = 34 + i / 255;
string ip = "188.10." + j + "." + i % 255;
int port = 26666;
IPAddress addr = IPAddress.Parse(ip);
IPEndPoint host = new IPEndPoint(addr, port);
list.Add(host);
}
System.Diagnostics.Stopwatch stop = new System.Diagnostics.Stopwatch();
stop.Start();
List<IPAddress> ips = new List<IPAddress>();
foreach (var item in list)
{
if (item.Address.ToString()=="188.10.34.140")
{
ips.Add(item.Address);
}
}
stop.Stop();
double mili = stop.Elapsed.TotalMilliseconds;
Console.WriteLine("loop: "+mili);
Console.WriteLine("loopCount: "+ips.Count);
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
List<IPAddress> lists = list.Where(e => e.Address.ToString() == "188.10.34.140").Select(e => e.Address).ToList<IPAddress>();
watch.Stop();
double milis = watch.Elapsed.TotalMilliseconds;
Console.WriteLine("lambda: "+milis);
Console.WriteLine("lambdaCount: "+lists.Count);
}
···
结果
loop: 0.4618
loopCount: 1
lambda: 0.6283
lambdaCount: 1
25500条
static void Main()
{
List<IPEndPoint> list = new List<IPEndPoint>();
for (int i =0;i<=25500;i++)
{
int j = 34 + i / 255;
string ip = "188.10." + j + "." + i % 255;
int port = 26666;
IPAddress addr = IPAddress.Parse(ip);
IPEndPoint host = new IPEndPoint(addr, port);
list.Add(host);
}
System.Diagnostics.Stopwatch stop = new System.Diagnostics.Stopwatch();
stop.Start();
List<IPAddress> ips = new List<IPAddress>();
foreach (var item in list)
{
if (item.Address.ToString()=="188.10.34.140")
{
ips.Add(item.Address);
}
}
stop.Stop();
double mili = stop.Elapsed.TotalMilliseconds;
Console.WriteLine("loop: "+mili);
Console.WriteLine("loopCount: "+ips.Count);
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
List<IPAddress> lists = list.Where(e => e.Address.ToString() == "188.10.34.140").Select(e => e.Address).ToList<IPAddress>();
watch.Stop();
double milis = watch.Elapsed.TotalMilliseconds;
Console.WriteLine("lambda: "+milis);
Console.WriteLine("lambdaCount: "+lists.Count);
}
结果
loop: 5.0348
loopCount: 1
lambda: 1.5531
lambdaCount: 1
猜想
Linq好像转化为stream再做处理,所以数据量少的时候比较占用时间,但是好省代码量。我也不懂Linq,只会用