Project

General

Profile

Actions

Feature #6256

closed

Slightly improve ruby_qsort performance

Added by MartinBosslet (Martin Bosslet) almost 12 years ago. Updated about 5 years ago.

Status:
Feedback
Target version:
-
[ruby-core:44136]

Description

Hi all,

I think I may have found a way to slightly improve the performance of ruby_qsort.
Quicksort running time is slightly decreased if the recursion depth (or in our
case, rather iteration depth) is bounded by leaving sub problems larger than or
equal to some cutoff bound k untouched and running Insertion Sort on these small
sub problems to finalize the sorting.

I experimented with this, but to no avail, only marginal improvements if any. Then
I remembered that instead of running Insertion Sort on each sub problem, it is
equivalent in terms of running time to run one single Insertion Sort on the whole
nearly sorted array as a final step. And in practice, this turns out to run faster
than without the optimization. In my tests, execution time dropped to about 95% on
average with an optimal cutoff (64-bit Fedora 15) [1].

Now this ain't the world - but it is faster, and I could very well imagine that there
is still room for improving my code. In my tests, the optimal cutoff seems to be ~13
for Integers and ~8 for Strings and Symbols. I imagine the more costly the comparisons,
the lower will be the optimal cutoff. I've tested only on 64 Bit yet, but I will do so
for 32 Bit, too.

If it turns out that this runs faster regardless of the architecture in use, with an
optimal cutoff yet to be determined, do you think this would be a useful addition?

I have attached a C extension for testing and discussing, it's mostly a one-to-one copy of
the code in util.c. I just added mmassign and insertion_sort plus the few lines that respect
the cutoff in rqsort (had to rename it, otherwise it would collide with the real "ruby_qsort").

-Martin

[1] https://github.com/emboss/hybridsort/blob/master/hybrid-sort-results


Files

hybridsort.tar.gz (3.93 KB) hybridsort.tar.gz MartinBosslet (Martin Bosslet), 04/05/2012 10:02 AM
rubyqisort.patch (5.04 KB) rubyqisort.patch MartinBosslet (Martin Bosslet), 04/07/2012 07:36 AM
rubyqisort4.patch (3.66 KB) rubyqisort4.patch MartinBosslet (Martin Bosslet), 04/07/2012 10:35 AM
rubyqisort5.patch (3.69 KB) rubyqisort5.patch MartinBosslet (Martin Bosslet), 04/07/2012 11:01 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0