Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How linq function OrderByDescending and OrderBy for string length works internally? Is it faster than doing it with loop?

My question is raised based on this question, I had posted an answer on that question..here

This is the code.

var lines = System.IO.File.ReadLines(@"C:\test.txt");
var Minimum = lines[0];//Default length set
var Maximum = "";

foreach (string line in lines)
{    
    if (Maximum.Length < line.Length)
    {
        Maximum = line;
    }

    if (Minimum.Length > line.Length)
    {
        Minimum = line;
    }
}

and alternative for this code using LINQ (My approach)

var lines = System.IO.File.ReadLines(@"C:\test.txt");
var Maximum = lines.OrderByDescending(a => a.Length).First().ToString();
var Minimum = lines.OrderBy(a => a.Length).First().ToString();

LINQ is easy to read and implement..

I want to know which one is good for performance. And how Linq work internally for OrderByDescending and OrderBy for ordering by length?

like image 223
sangram parmar Avatar asked Jun 25 '15 07:06

sangram parmar


People also ask

What is the use of OrderBy in C#?

In a query expression, the orderby clause causes the returned sequence or subsequence (group) to be sorted in either ascending or descending order. Multiple keys can be specified in order to perform one or more secondary sort operations. The sorting is performed by the default comparer for the type of the element.

How does OrderBy work in LINQ?

Introduction to LINQ orderby. LINQ-OrderBy sorts the values in the collection on the basis of the specified field in ascending or descending manner. By default, it sorts the collection of elements in ascending order.

What is difference between OrderBy and ThenBy in LINQ?

The OrderBy() Method, first sort the elements of the sequence or collection in ascending order after that ThenBy() method is used to again sort the result of OrderBy() method in ascending order.

Is LINQ OrderBy ascending?

In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order.


1 Answers

You can read the source code for OrderBy.

Stop doing micro-optimizing or premature-optimization on your code. Try to write code that performs correctly, then if you face a performance problem later then profile your application and see where is the problem. If you have a piece of code which have performance problem due to finding the shortest and longest string then start to optimize this part.

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3% - Donald Knuth

File.ReadLines is returning an IEnumerable<string>, It means that if you do a foreach over it it will return data to you one by one. I think the best performance improvement you can do here is to improve the reading of file from the disk. If it is small enough to load the whole file into memory use File.ReadAllLines, if it is not try reading the file in big chunks that fits in memory. Reading a file line by line will cause performance degradation due to I/O operation from disk. So the problem here is not how LINQ or loop perform, The problem is in number of disk reads.

like image 151
Hamid Pourjam Avatar answered Oct 19 '22 21:10

Hamid Pourjam