[ANN] Sort::Key 0.02

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default [ANN] Sort::Key 0.02

    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.

    Salvador Fandino Guest

  2. Similar Questions and Discussions

    1. Sort
      Hi, I want to write a perl script to do something like this Abc 12.8 8 "left" 1 15.7 Def 13.8 9 "top" 0 19.7
    2. Sort by id
      Now I simple forum by Dreamweaver i use "repeat region" for show data in database . It show like this 1 2 3 4 but i want to show
    3. Sort bug
      It appears that the runtime cannot sort a datagrid column when the items in the column contain a NaN or Infinity. Is there a way to override the...
    4. memory sort and disk sort
      I check the sysprofile table and find there are 700 times disk sort, I think it is lack of sort memory. I want to turn all the disk sort into the...
    5. Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB.
      Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp...
  3. #2

    Default Re: [ANN] Sort::Key 0.02

    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];





    --
    [url]http://www.goldfisch.at/know_list[/url]
    peter pilsl Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139