我想检查一个列表是否包含在另一个列表中。
我的代码
List<string> words = new(){"the","play","in","on"};
string txt="Barcelona is playing against Manchester tonight.";
List<string> txtList=txt.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList();
bool exist = false;
Stopwatch sw;
Stopwatch sw2;
sw = Stopwatch.StartNew();
for (int i=0; i<1000 ;i++)
{
exist = words.Exists(w => txtList.Exists(t => t.Contains(w)));
}
sw.Stop();
Console.WriteLine("ExistResult: "+ exist + Environment.NewLine + "ExistTime: " + sw.ElapsedTicks);
sw2 = Stopwatch.StartNew();
for (int i=0; i<1000 ;i++)
{
exist = words.Any(w => txtList.Any(t => t.Contains(w)));
}
sw2.Stop();
Console.WriteLine("AnyResult: "+ exist + Environment.NewLine + "AnyTime: " + sw2.ElapsedTicks);
结果
存在结果:True
存在时间:2574053
任何结果:正确
随时:1265826
方法 Any 快两倍
但是为什么 Visual Studio 建议我使用 Exist 方法?
基准测试很困难,你永远不应该尝试自己动手做基准测试。我什至无法开始告诉你你搞砸了数百万条小规则中的哪一条,但适当的基准测试几乎可以显示你所期望的:
| Method | N | Mean | Error | StdDev |
|------- |------ |----------:|---------:|---------:|
| Exists | 1000 | 27.17 ms | 0.257 ms | 0.241 ms |
| Any | 1000 | 53.19 ms | 0.614 ms | 0.575 ms |
| Exists | 5000 | 132.33 ms | 0.962 ms | 0.852 ms |
| Any | 5000 | 259.50 ms | 1.104 ms | 0.979 ms |
| Exists | 10000 | 245.17 ms | 2.417 ms | 2.261 ms |
| Any | 10000 | 521.30 ms | 4.468 ms | 4.179 ms |
换句话说,
Exists
的速度大约是Any
的两倍。