6

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

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.

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!”

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);

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);

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?

Community
  • 1
  • 1
TalkingCode
  • 13,407
  • 27
  • 102
  • 147
  • p.s., Typically when doing benchmarks, you need to perform the test many times and average out the times to get your result. One iteration is not enough. – Jeff Mercado Jul 02 '12 at 17:22

1 Answers1

16

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.

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

Community
  • 1
  • 1
Polynomial
  • 27,674
  • 12
  • 80
  • 107
  • It's not broken, it's just limited to 10k+ rep users due to the deleted status. Since a bunch of people voted to undelete, it's now back. – Polynomial Jan 05 '14 at 00:12
  • What is the a one-time initialization delay that you mention? From what I've read [here](http://stackoverflow.com/a/12820080/1057791) the reasons that linq is a bit slower is because of extra function calls to the enumerator and the predicate and extra object allocations and that happens per iteration? – BornToCode Feb 08 '16 at 06:21
  • @BornToCode I'm not sure, exactly, but my suspicion is that the `FirstOrDefault' call actually calls a bunch of other stuff down the stack before actually starting the search loop. – Polynomial Feb 08 '16 at 16:43