Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are we compromising performance for achieving code readability when using LINQ?

I have tried checking LINQ performance against conventional brute-force search and it seems LINQ is much slower than the brute force approach and using Contains method takes even longer!

The following is my code:

class Program
{
  int[] array;
  public Program(int size)
  {
     array = new int[size];
     for (int i = 0; i < size; i++)
     {
        array[i] = size - i;
     }
  }
  public void BruteForceSearch(int toBeSearched)
  {
     foreach (int i in array)
     {
        if (i == toBeSearched)
        {
           return;
        }
     }
  }
  public void SearchViaContainsMethod(int toBeSearched)
  {
     if (array.Contains(toBeSearched)) { return; }
  }
  public void SearchViaLINQ(int toBeSearched)
  {
     var x = from a in array
             where a == toBeSearched
             select a;
  }
  static void Main(string[] args)
  {
     Program p = new Program(100000);
     using (new OperationTimer("BruteForceSearch"))
     {
        p.BruteForceSearch(0);
     }
     using (new OperationTimer("SearchViaContainsMethod"))
     {
        p.SearchViaContainsMethod(0);
     }
     using (new OperationTimer("SearchViaLINQ"))
     {
        p.SearchViaLINQ(0);
     }
     Console.Read();
  }



class OperationTimer : IDisposable
{
  private Stopwatch m_stopwatch;
  private String m_text;

  public OperationTimer(String text)
  {
     m_text = text;
     m_stopwatch = Stopwatch.StartNew();
  }

  public void Dispose()
  {
     Console.WriteLine("{0} {1}",
        (m_stopwatch.Elapsed), m_text);
  }
}

Output:

00:00:00.0009032 BruteForceSearch
00:00:00.0068469 SearchViaContainsMethod
00:00:00.0032512 SearchViaLINQ

I can understand the time taken is very low for everything and with the current processor's speed using the LINQ may not create memory overhead.

I would like to know, when processing a huge set of data, will the speed will go exponentially higher and we really have to compromise on the speed for code-readability?

Please help understanding this.

(Courtesy: OperationTimer type was taken from CLR Via C# by Jeffery Ritcher.

like image 517
Prasanna Avatar asked Apr 07 '26 07:04

Prasanna


1 Answers

Yes, we are doing exactly that: choosing code readability over performance. But in the vast majority of cases, it's really better to have the improved code readability. If you're working with very limited memory or CPU, or in a very performance-sensitive environment, it might be an issue. But if you were in such an environment, you shouldn't have chosen a high-level language/platform like C#/.NET.

If you profile your application and find that the overhead of LINQ is adding a significant amount of time for some particular operation, then you can rewrite it to be more efficient. To worry about it before that point is just premature micro-optimization, and does more harm than good.

like image 99
Tim S. Avatar answered Apr 08 '26 21:04

Tim S.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!