[#44036] [ruby-trunk - Feature #6242][Open] Ruby should support lists — "shugo (Shugo Maeda)" <redmine@...>

20 messages 2012/04/01

[#44084] [ruby-trunk - Bug #6246][Open] 1.9.3-p125 intermittent segfault — "jshow (Jodi Showers)" <jodi@...>

22 messages 2012/04/02

[#44156] [ruby-trunk - Feature #6265][Open] Remove 'useless' 'concatenation' syntax — "rosenfeld (Rodrigo Rosenfeld Rosas)" <rr.rosas@...>

45 messages 2012/04/06

[#44163] [ruby-trunk - Bug #6266][Open] encoding related exception with recent integrated psych — "jonforums (Jon Forums)" <redmine@...>

10 messages 2012/04/06

[#44303] [ruby-trunk - Feature #6284][Open] Add composition for procs — "pabloh (Pablo Herrero)" <pablodherrero@...>

57 messages 2012/04/12

[#44349] [ruby-trunk - Feature #6293][Open] new queue / blocking queues — "tenderlovemaking (Aaron Patterson)" <aaron@...>

10 messages 2012/04/13

[#44402] [ruby-trunk - Feature #6308][Open] Eliminate delegation from WeakRef — "headius (Charles Nutter)" <headius@...>

20 messages 2012/04/17

[#44403] [ruby-trunk - Feature #6309][Open] Add a reference queue for weak references — "headius (Charles Nutter)" <headius@...>

15 messages 2012/04/17

[#44533] [ruby-trunk - Bug #6341][Open] SIGSEGV: Thread.new { fork { GC.start } }.join — "rudolf (r stu3)" <redmine@...>

24 messages 2012/04/22

[#44630] [ruby-trunk - Feature #6361][Open] Bitwise string operations — "MartinBosslet (Martin Bosslet)" <Martin.Bosslet@...>

31 messages 2012/04/26

[#44648] [ruby-trunk - Feature #6367][Open] #same? for Enumerable — "prijutme4ty (Ilya Vorontsov)" <prijutme4ty@...>

16 messages 2012/04/26

[#44704] [ruby-trunk - Feature #6373][Open] public #self — "trans (Thomas Sawyer)" <transfire@...>

61 messages 2012/04/27

[#44748] [ruby-trunk - Feature #6376][Open] Feature lookup and checking if feature is loaded — "trans (Thomas Sawyer)" <transfire@...>

13 messages 2012/04/28

[ruby-core:44144] [ruby-trunk - Feature #6256] Slightly improve ruby_qsort performance

From: duerst (Martin Dürst) <duerst@...>
Date: 2012-04-05 10:04:19 UTC
List: ruby-core #44144
Issue #6256 has been updated by duerst (Martin D端rst).


I haven't looked at the sources, but this exact optimization is very well known in the literature, and I'm showing it to my students in a lecture on algorithms and data structures every year (see e.g. http://www.sw.it.aoyama.ac.jp/2011/DA/programs/6qsort.rb, sorry, comments are in Japanese). I'm quite a bit surprised that this optimization isn't yet made in Ruby. The only potential problem is that the final insertion sort may mask errors in the quicksort, but as Ruby's current quicksort is working well, that should not be a problem. And a cutoff of about 10 is what's generally claimed to be a good value. The speed gains/losses when shifting the cutoff value by just a little bit shouldn't be very big.

So I'm definitely encouraging adoption of this improvement.
----------------------------------------
Feature #6256: Slightly improve ruby_qsort performance
https://bugs.ruby-lang.org/issues/6256#change-25667

Author: MartinBosslet (Martin Bosslet)
Status: Open
Priority: Low
Assignee: 
Category: core
Target version: 2.0.0


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


-- 
http://bugs.ruby-lang.org/

In This Thread