tholbroo had mentioned to me that if I'm doing a regular sort on Class::DBI objects, I can save a lot of time by doing a Schwartzian transform. I didn't think that there would be much of a penalty on small sets, but I decided to do some benchmarking (using the CPANTS DB):
package CPANTS::Distro::Kwalitee;
use strict;
use warnings;
use base qw( Class::DBI );
__PACKAGE__->connection( 'dbi:SQLite:dbname=cpants.db' );
__PACKAGE__->table( 'kwalitee' );
__PACKAGE__->columns(
All => qw(
distid
kwalitee
extractable
extracts_nicely
has_buildtool
has_manifest
has_meta_yml
has_proper_version
has_readme
has_test_pod
has_test_pod_coverage
has_tests
has_version
is_prereq
no_cpants_errors
no_pod_errors
no_symlinks
proper_libs
use_strict
)
);
package main;
use strict;
use warnings;
use Benchmark qw( cmpthese );
my @distros = CPANTS::Distro::Kwalitee->retrieve_all;
@distros = @distros[ 0..99 ];
printf( "Sorting %d rows...\n", scalar @distros );
cmpthese(
1000,
{
regular_sort => \®ular_sort,
schwartz_sort => \&schwartz_sort
}
);
sub regular_sort {
@distros = sort {
$a->kwalitee <=> $b->kwalitee
} @distros;
}
sub schwartz_sort {
@distros = map {
$_->[ 0 ]
} sort {
$a->[ 1 ] <=> $b->[ 1 ]
} map {
[ $_, $_->kwalitee ]
} @distros;
}
__END__
C:\cdbibench>perl bench.pl
Sorting 100 rows...
Rate regular_sort schwartz_sort
regular_sort 102/s -- -47%
schwartz_sort 195/s 90% --
Interesting. tholbroo++
That's not surprising (Score:2)
Sort in the database (Score:2)
Class::DBI considered a hinderance (Score:1)
Yeah, what autarch said.
This is why gave up Class::DBI – it very nearly forces you to do on the Perl side what should be done at the SQL level because it makes it way too difficult to do the latter. First you run into a wall of no documentation, then you often have to put up with annoying verbiage. After reading the source for 15 minutes, I think the following will do what you need:
Try Sort::Key (Score:1)
You can try Sort::Key that I released some days ago on CPAN, its like the Schwartzian transform, but implemented in C so a bit faster.