LINQ是否比简单循环慢得多? [英] Is LINQ much slower than a simple loop?

查看:196
本文介绍了LINQ是否比简单循环慢得多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
是否应该避免LINQ,因为它很慢?

Possible Duplicate:
Should LINQ be avoided because it's slow?

我爱LINQ.正如我今天在另一篇文章中所读到的,这是切成薄片以来最好的事情",我完全同意.但是在我工作的公司中,其他所有人似乎都讨厌LINQ.

I love LINQ. As I read in another post today "it's the best thing since sliced bread" and I totally agree. But at the company I work everyone else seems to hate LINQ.

几周前,我第一次使用ReSharper,而在我编码的时候,ReSharper突然告诉我,我的foreach循环可以转换为LINQ表达式.这对我来说就像魔术,我向同事展示了.令我惊讶的是,他说:我希望它可以反过来工作,并将LINQ变成循环.那会快得多!"

A few weeks ago I was using ReSharper for the first time and while I was coding ReSharper suddenly told me my foreach-loop could be converted into a LINQ expression. This was like magic to me and I showed my colleague. Much to my suprise he said "I wish it would work the other way around and turn LINQ into loops. That would be much faster!"

那么LINQ-to-Objects真的那么慢吗?我自己试了一下. 当我几次运行以下示例时,我得到了大约350的过去的滴答声".

So is LINQ-to-Objects really so slow? I tried out myself. When I run the following sample a few times I get Elapsed Ticks around 350.

        Stopwatch sw = new Stopwatch();

        List<Person> personList = new List<Person>();
        for (int i = 0; i < 5000; i++)
        {
            Person p = new Person() {ID = i};
            personList.Add(p);
        }

        sw.Start();

        Person searchPerson = null;

        foreach (Person person in personList)
        {
            if (person.ID == 4321)
            {
                searchPerson = person;
                break;
            }
        }

        sw.Stop();

        Console.WriteLine(sw.ElapsedTicks);

如果我将循环更改为LINQ查询(Resharper会为我完成),我得到的ElapsedTicks大约为900.是循环的两倍多.

If I change the loop to a LINQ-Query (Resharper will do that for me) I get ElapsedTicks around 900. More than twice as much as with the loop.

Person searchPerson = personList.FirstOrDefault(person => person.ID == 4321);

似乎LINQ确实速度较慢,如果经常使用它,可能会成为问题.在我们公司,我们拥有大量数据.那么避免LINQ是正确的决定还是我们做错了什么?

As is seems LINQ is indeed slower and if you use it a lot this could be an issue. And at our company we have lots of data. So is it the right decision to avoid LINQ or are we doing something wrong?

推荐答案

是的,它比较慢.但是,该延迟的一部分是一次性初始化延迟,而不是每次迭代延迟.在100k迭代循环中,百分比差异要小得多.

Yes, it's slower. However, a portion of that delay is a one-time initialisation delay, rather than a per-iteration delay. The percentage difference is quite a bit lower on a 100k iteration loop.

要点是,开发人员的时间要比代码中的少量性能损失贵得多,这要花很多时间,除非客户要求您抱怨性能问题.编写可读且可维护的代码比对代码进行微优化更重要.

The point to take away is that developer time is significantly more expensive than a small performance loss in your code, unless the customer is calling you up to complain about performance problems. Writing readable and maintainable code is much more important than micro-optimising your code.

正如埃里克·利珀特(Eric Lippert)如此完美地指出,只有在以下情况下才应避免使用LINQ:不够快.

As Eric Lippert pointed out so perfectly, LINQ should only be avoided if it's not fast enough.

这篇关于LINQ是否比简单循环慢得多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆