I have a feeling using perl's sort might not be the greatest plan in this situation. I'm sure there is a module that does an inplace sort that will be faster (I've not needed it so i haven't looked). Pushing 500k items on the stack to sort them and then popping them back off is IMO going to really hurt for long lists. An inline sort would avoid that overhead. The actually oid_lex_sort() code actually does this an extra time. In my implementation I avoid part of it by passing the array in as a ref.

With regard to the actual implementation of ST that oid_grt_sort uses, I think the GRT might win. Its hard to say. Avoiding the array creation I should think will help with speed and I know will help for memory footprint. Overall I'd guess that this would be faster, but I haven't benchmarked it at all. Actually for that matter I've minimally tested this, but it appears to work and should give you an idea how it could be done. Also I've included the code for oid_lex_sort() which really you should have done yourself. :-)

use strict; use warnings; sub oid_lex_sort(@) { return @_ unless (@_ > 1); map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { my $oid = $_; $oid =~ s/^\.//o; $oid =~ s/ /\.0/og; [$_, pack('N*', split('\.', $oid))] } @_; } sub oid_grt_sort { my $ary=shift; map { my $len=unpack('N*',substr($_,-4)); substr($_,-$len-4,$len) } sort map { my $oid=$_; $oid =~ s/^\.//; $oid =~ s/ /.0/g; pack('N*', split(/\./, $oid)) . $_ . pack('N*', length($_)); } @$ary; } my @o=('.1.3.4.5.7','1.2 3.4.8','3.2 9.1 7','1 3 4 5 9'); print join "\n",qw(grt:),oid_grt_sort(\@o),""; print join "\n",qw(---- lex:),oid_lex_sort(@o),"";

HTH

---
$world=~s/war/peace/g


In reply to Re: a more efficient lexicographical sort? (grt) by demerphq
in thread a more efficient lexicographical sort? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.