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
|