Custom Quicksort vs Library Sort in C#

Last night I was thinking about the performance of a custom, program-defined sorting routine versus the performance of a built-in, library-defined sorting routine. (Yes, my life is pretty sad).

I had two hypotheses. First, a custom sort routine would be slightly faster than a library routine because the custom version could avoid unnecessary error-checking. Second, a library sort routine would be slightly faster because it can do all kinds of low-level optimizations.


Years ago I worked on analyzing the performance of Internet Explorer. It turns out that the only way to know about performance is to run experiments. So, I coded up a custom quicksort routine using the C# language. Then I measured the time to sort an array with 100,000 random values using my custom quicksort and also using the built-in library Array.Sort() routine.

The library sort routine took about 26 milliseconds and the custom sort routine took about 13 milliseconds. My conclusion is that the library sort routine is better except in situations where you need some kind of custom sorting behavior, or if you intend for the program that uses the sort routine to be implemented in several different languages.

This entry was posted in Machine Learning. Bookmark the permalink.