In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] 
(Randal L. Schwartz) wrote:

> >>>>> "brian" == brian d foy <[EMAIL PROTECTED]> writes:

> brian> In article <[EMAIL PROTECTED]>, 
> brian> [EMAIL PROTECTED] (Darin Weeks) wrote:

> brian> you can use a schwartzian transform to pre-compute the lengths,
> brian> then sort on the lengths and recover the original string.

> Except that "length" is a cheap function, so you've now made
> an expensive tradeoff that would take many hundreds of elements to
> finally equalize out.  I'd just use the length directly:

indeed.

brian[1094]$ cat test.pl
#!/usr/bin/perl

use Benchmark qw(countit timestr);

@elements = <>;
print "There are " . @elements . " elements\n";

my $map = <<'HERE';
@sorted = map { $_->[0] }
        sort { $b->[1] <=> $a->[1] }
        map  { [ $_, length ] }
        @elements;
HERE

my $sort = <<'HERE';
@sorted = sort { length $b <=> length $a } @elements
HERE

my $t = countit(30, $map);
my $count = $t->iters ;
print "$count loops of map took: ", timestr($t), "\n";

my $t2 = countit(30, $sort);
my $count2 = $t2->iters;
print "$count2 loops of sort took: ", timestr($t2), "\n";

brian[1094]$ perl test.pl foo
There are 38444 elements
31 loops of map took: 31 wallclock secs (30.68 usr +  0.00 sys = 30.68 CPU) @  1.01/s 
(n=31)
105 loops of sort took: 32 wallclock secs (31.41 usr +  0.00 sys = 31.41 CPU) @  
3.34/s (n=105)
-- 
brian d foy <[EMAIL PROTECTED]> - Perl services for hire
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to