in reply to Re^4: Accessing $a and $b of 'sort' across package boundaries
in thread Accessing $a and $b of 'sort' across package boundaries
Actually,
sort gs::long_strings_first($a,$b) @$raw_hash_keys_ar;
won't run. You mean
sort { gs::long_strings_first($a,$b) } @$raw_hash_keys_ar;
Like I said, I think there's a substantial difference in performance. Let's put it to the test:
use strict; use warnings; package gs; sub long_strings_first($$) { length $_[1] <=> length $_[0] || lc $_[1] cmp lc $_[0] } sub long_strings_first_no_proto { length $_[1] <=> length $_[0] || lc $_[1] cmp lc $_[0] } package main; use Benchmark qw( cmpthese ); my @data; { open(my $data, '<', $0); local $/; @data = split(/\s+/, <$data>); } cmpthese(-3, { implicit => \&implicit, explicit => \&explicit, explicit_np => \&explicit_np, });
sub implicit { my @array = sort gs::long_strings_first @data; } sub explicit { my @array = sort { gs::long_strings_first($a,$b) } @data; } sub explicit_np { my @array = sort { gs::long_strings_first_no_proto($a,$b) } @data; } __END__ Rate explicit_np explicit implicit explicit_np 606/s -- -1% -49% explicit 610/s 1% -- -49% implicit 1187/s 96% 95% --
|
|---|