相关疑难解决方法(0)

对于Linq - 性能与未来

非常简短的问题.我有一个随机排序的大字符串数组(100K +条目),我想找到所需字符串的第一次出现.我有两个解决方案.

从我读到我可以猜到的是'for循环'目前会提供略微更好的性能(但这个余量总是会改变),但我也发现linq版本更具可读性.总的来说哪种方法通常被认为是目前最好的编码实践,为什么?

string matchString = "dsf897sdf78";
int matchIndex = -1;
for(int i=0; i<array.length; i++)
{
    if(array[i]==matchString)
    {
        matchIndex = i;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

int matchIndex = array.Select((r, i) => new { value = r, index = i })
                         .Where(t => t.value == matchString)
                         .Select(s => s.index).First();
Run Code Online (Sandbox Code Playgroud)

c# linq performance

36
推荐指数
3
解决办法
3万
查看次数

标签 统计

c# ×1

linq ×1

performance ×1