[tpm] OID sorting

Uri Guttman uri at stemsystems.com
Sat May 2 20:58:31 PDT 2009


>>>>> "FH" == Fulko Hew <fulko.hew at gmail.com> writes:

i couldn't resist!

  FH> sub fulko_sort_optimized {
  FH>     my @oids = sort
  FH>                 map { join ".",
  FH>                     map { sprintf ("%8d", $_); } split /\./;
  FH>                 } @_;

that is a form of packed sort. you just pad each oid to 8 chars with
leading blanks.

  FH>     for (my $i = 0; $i < scalar(@oids); $i++) {
  FH>         $oids[$i] =~ s/ //g;
  FH>     }

blech! i hate useless c for loops. actually i hate seeing c for loops in
perl in general. they are so rarely needed. and i mean rare as in maybe
10 in 10k lines of code.

	s/ //g for oids ;

and for good measure this may work using the same logic:

	s/(\d+)/sprintf( '%8s', $1 )/ge for oids ;
	@oids = sort @oids ;
	s/ //g for oids ;
	return @oids ;

that is a lot cleaner and easier to read.  if there was a decent way for
map and s/// to return the modified string and not the result of s/// it
would be easy in one expression. this is the way it can work now:

	return
	map { s/ //g ; $_ }
	sort
	map { s/(\d+)/sprintf( '%8s', $1 )/ge ; $_ }
	@_ ;

note that i use %8s as there is no need to convert any oid part to an
integer. that should be a bit faster too.

also if the data size is large enough, the first version which mungs @oids
directly may be faster than the map version which has to allocate large
temp lists. hell, it may be faster for small sets too. you should
benchmark these as well.

uri

-- 
Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


More information about the toronto-pm mailing list