using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public class MyClass
{
public int Id { get; private set; }
public string Name { get; private set; }
public MyClass(int id)
{
this.Id = id;
this.Name = id.ToString();
}
}
class Program
{
static void Main(string[] args)
{
var arrayA
= Enumerable.
Range(1, 10000).
Select(_
=> new MyClass
(rand.
Next(1, 100))).
ToArray(); var arrayB
= Enumerable.
Range(1, 10000).
Select(_
=> new MyClass
(rand.
Next(1, 100))).
ToArray();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var sw = Stopwatch.StartNew();
var ret = (
from a in arrayA
join b in arrayB on a.Id equals b.Id
select a.Name).ToList();
sw.Stop();
Console.WriteLine(sw.Elapsed);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
sw.Reset(); sw.Start();
var ret2 = new List<string>();
var lookup = arrayB.ToLookup(x => x.Id);
foreach (var itemA in arrayA)
{
foreach (var itemB in lookup[itemA.Id])
{
ret2.Add(itemA.Name);
}
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
Console.Write("check result : ");
Console.WriteLine(ret.SequenceEqual(ret2));
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkRpYWdub3N0aWNzOwp1c2luZyBTeXN0ZW0uTGlucTsKCnB1YmxpYyBjbGFzcyBNeUNsYXNzCnsKICAgIHB1YmxpYyBpbnQgSWQgeyBnZXQ7IHByaXZhdGUgc2V0OyB9CiAgICBwdWJsaWMgc3RyaW5nIE5hbWUgeyBnZXQ7IHByaXZhdGUgc2V0OyB9CgogICAgcHVibGljIE15Q2xhc3MoaW50IGlkKQogICAgewogICAgICAgIHRoaXMuSWQgPSBpZDsKICAgICAgICB0aGlzLk5hbWUgPSBpZC5Ub1N0cmluZygpOwogICAgfQp9CgpjbGFzcyBQcm9ncmFtCnsKICAgIHN0YXRpYyB2b2lkIE1haW4oc3RyaW5nW10gYXJncykKICAgIHs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Gl0ZW1CIGluIGxvb2t1cFtpdGVtQS5JZF0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHJldDIuQWRkKGl0ZW1BLk5hbWUpOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBzdy5TdG9wKCk7CiAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoc3cuRWxhcHNlZCk7CgogICAgICAgIENvbnNvbGUuV3JpdGUoImNoZWNrIHJlc3VsdCA6ICIpOwogICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKHJldC5TZXF1ZW5jZUVxdWFsKHJldDIpKTsKICAgIH0KfQ==
prog.cs(48,26): warning CS0168: The variable `itemB' is declared but never used
Compilation succeeded - 1 warning(s)
00:00:00.1688902
00:00:00.0924882
check result : True