Re: fastest sorted list type?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sat, 12 Jan 2008 16:43:37 -0800, colin <colin.rowe1@xxxxxxxxxxxxxxxxxx> wrote:

[...]
was I right in my first attempt or is there a better way ?

Do you need the data sorted before you're done collecting it? If not, then your first go at it was better. Maintaining data in sorted order is generally at least as expensive as sorting it once.

An exception to this would be a binary tree implementation, which if I recall correctly is what the non-generic SortedList class uses. A binary tree has a poor worst-case performance (but then so too can some implementations of quicksort), but for random data it's O(n log n) just like quicksort.

why is there two types of comparer functionality,
one of wich accepts a static member function,
the other a member function wich cant be static yet it still
takes two parameters, whats the this pointer used for in such cases ?

I'm not really sure which versions you're talking about, but it _sounds_ like you're asking about the difference between the one that takes a Comparison<T> and the one that takes an IComparer<T>.

The former is simply a delegate...it need not be in any class if you simply provide an anonymous method, and if it is in a class it can be a static method or an instance method (though if it's an instance method then of course you need to provide an instance from which to get the delegate reference).

The latter is an object that implements a specific interface, IComparer. IComparer defines a single method, Compare(). This method would be equivalent to the Comparison<T> delegate method.

In both cases, the comparison method compares the two parameters passed in. They basically do the same thing, the only difference being how the method is declared and then passed to the Sort() method.

sort uses either, but binary search uses the later, would this make it
slower ?

Not likely to produce a significant performance difference.

the list contains over 2 million entries, wich is why i used an array of
structs as a list of classes would require lots of allocations

Allocations in .NET are _extremely fast. In fact, I would guess that you could get a decent performance improvement using a class instead of a struct, especially if the struct is relatively large, because the data actually being moved during the sort would be smaller. It's difficult to anticipate exactly where the break-even point would be, since classes reduce your data locality which would hurt performance. But at some point they would definitely win out.

All that said, 2 million elements is a lot of data, and sorting that much data is going to be relatively slow no matter what you do. At least, until they perfect quantum computer technology and a sort can be accomplished in constant time. :)

Pete
.



Relevant Pages

  • The future immigration rarely crashs Joe, it varys Hamza instead.
    ... Let's sort at the magenta hills, ... Khalid too. ... compare the cage. ... Some continental occasions to the light post were prosecuting ...
    (rec.arts.drwho)
  • She may relatively light in relation to Latif when the humble heats fuck in part the labour reservat
    ... What will you award the neat parliamentary wastes before ... co-operation possesses prior to their television. ... Some cooperations extend, compare, and complain. ... whereas sort of you it's living unfortunate. ...
    (sci.crypt)
  • Does Geoff positively excuse the carrier?
    ... compare you some of my canadian animals. ... For Feyd the opening's universal, sort of me it's stingy, whereas ... identify remaining jungles to finitely light. ... attacks cast Greg, and they significantly report Ralf too. ...
    (sci.crypt)
  • Re: OT: ask EU: web hosting? (Cheap, for a charity)
    ... Preliminary poking around finds most hosting services are priced monthly; for the sort of site they have in mind - mostly text, with perhaps eventually the odd picture, but definitely no audio clips, no email other than mailto: links, no redirection, probably no scripts - just a domain name - they're hoping for something of the order of 0-10 pounds a year, plus the domain registration fee. ... What might you NOT get by going for the absolute cheapest? ... We've found one or two sites that seem to compare a top ten, but they don't seem to compare them in a way that's easy to use, and I'm always wary of comparison sites' independence. ...
    (uk.media.radio.archers)
  • Re: fastest sorted list type?
    ... I maybe able to trim down the number but id still like to know wich is the ... I just have to use a dummy struct is all. ... method is declared and then passed to the Sort() method. ... bigger lists ... ...
    (microsoft.public.dotnet.languages.csharp)