For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > April 2005 > [ANN] Sort::Key 0.02









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author [ANN] Sort::Key 0.02
Salvador Fandino

2005-04-28, 8:56 am

Hi,

I have released Sort::Key 0.02, a module for sorting objects by some key.

It's really fast, usually much faster than perl core sort function and
even than other popular methods like the Schwartzian or the GRM
transforms (and BTW, easier to use).


Comments, bug reports, etc., are welcome!


The docs follow...

NAME
Sort::Key - Perl extension for sorting objects by some key

SYNOPSIS
use Sort::Key;

@by_name = keysort { "$_->{surname} $_->{name}" } @people;
@by_age = nkeysort { $_->{age} } @people;
@by_sons = ikeysort { $_->{sons} } @people;

DESCRIPTION
Sort::Key provides a set of functions to sort object
arrays by some (calculated) key value.

Usually, it is faster and uses less memory than other
alternatives implemented around perl sort function.

EXPORT
This package exports these functions:

keysort { CALC_KEY } @array
returns the elements on @array sorted by the key
calculated applying "{ CALC_KEY }" to them.

Inside "{ CALC_KEY }", the object is available as
$_.

For example:

@a=({name=>john, surname=>smith},
{name=>paul, surname=>belvedere});
@by_name=keysort {$_->{name}} @a;

lkeysort { CALC_KEY } @array
similar to keysort but takes into account locale
configuration when comparing keys.

nkeysort { CALC_KEY } @array
similar to keysort but compares the keys numerically
instead of as strings.

ikeysort { CALC_KEY } @array
similar to keysort but automatically converts the
keys to integer values and compares them
numerically.

SEE ALSO
perl sort function

AUTHOR
Salvador Fandino, <sfandino@yahoo.com>

COPYRIGHT AND LICENSE
Copyright (C) 2005 by Salvador Fandino

This library is free software; you can redistribute it
and/or modify it under the same terms as Perl itself,
either Perl version 5.8.4 or, at your option, any later
version of Perl 5 you may have available.

peter pilsl

2005-04-28, 3:58 pm

Salvador Fandino wrote:
>Comments, bug reports, etc., are welcome!


a first test is impressive:

create 0.09837
sort1 0.638449 <- perl-sort
sort2 0.116141 <- your sort


best,
peter


#!/usr/bin/perl -w

use Time::HiRes qw(gettimeofday tv_interval);
use Sort::Key;

my $t0 = [gettimeofday];
print "create\t";
my $x;
foreach (0..20000) {
$x->{$_}=rand();
}

$elapsed = tv_interval ( $t0 );
print $elapsed,"\n";
$t0 = [gettimeofday];

print "sort1\t";
my @r1=sort {$x->{$a} <=> $x->{$b}} (0..20000);
$elapsed = tv_interval ( $t0 );
print $elapsed,"\n";
$t0 = [gettimeofday];

print "sort2\t";
my @r2=nkeysort {$x->{$_}} (0..20000);
$elapsed = tv_interval ( $t0 );
print $elapsed,"\n";
$t0 = [gettimeofday];





--
http://www.goldfisch.at/know_list
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com