Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# faster sorting than SortedList<>

we have a

SortedList<Resource, Resource> resources =
    new SortedList<Resource, Resource>(new ResourceIdle());

that we use in our simulation. This list of resources is initialised in this way because we want to pass different comparers at any point in time. First problem we have is that the SortedList<> requires an extra comparison within the comparer so that we can add different instances of Resource with the same properties. For example if the Comparer looks like:

public int Compare(Resource x, Resource y)  
{  
    int priority1 = x.Priority;    
    int priority2 = y.Priority;    

    if (priority1 > priority2) { 
      return -1;  
    } else if (priority1 < priority2) {  
      return 1;  
    } else {  
      return (x.Id.CompareTo(y.Id));  
    }  
}  

then we have to make the extra comparison when priorities are the same otherwise we get back an exception for an entry with the same key. So my question is, is there another way of achieving this? And as a secondary question is there anything faster than the SortedList<> for ordering large number of objects?

like image 915
Dimitris Avatar asked Sep 28 '10 19:09

Dimitris


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

Well, SortedDictionary<,> has different performance characteristics - it depends on what you're doing with it. MSDN has quite a lot of detail comparing the two:

The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this respect, it is similar to the SortedList<TKey, TValue> generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:

  • SortedList<TKey, TValue> uses less memory than SortedDictionary<TKey, TValue>.
  • SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data: O(log n) as opposed to O(n) for SortedList<TKey, TValue>.
  • If the list is populated all at once from sorted data, SortedList<TKey, TValue> is faster than SortedDictionary<TKey, TValue>.
like image 194
Jon Skeet Avatar answered Sep 24 '22 16:09

Jon Skeet


it is a requirement that the list is always sorted at any time? if not, it would definitely be faster to only sort on demand. another idea is to have the sorting be done by an "OrderPriority" which is a combination of the Priority and ID fields, therefore only one comparison needs to be made:

int OrderPriority { get { return Priority * MAX_ID + ID; } }

this assumes that the IDs do not get too large...

like image 34
Alex Lo Avatar answered Sep 23 '22 16:09

Alex Lo