#!/usr/local/bin/perl

    use Sort::Maker;
     use Benchmark 'cmpthese';

     sub build_data {
         my @r_order =
           map { ('_', 0..9, 'A'..'Z', 'a'..'z')[rand 63] } 1..shift;
         my @unsorted =
           map qq|<table><tr><td meascode="$_"></td></tr></table>|,
           @r_order;
         my @c_order;
         push @c_order, splice( @r_order, rand @r_order, 1 )
           while @r_order;
         join( '', @c_order ), @unsorted
     }

     my ($cost_order, @unsorted) = build_data(2);

    my $sorter = make_sorter(
               'GRT',
               init_code   => "my \$cost_order = '$cost_order';",
               signed      => 1,
               string_data => 1,
               number      => q{ /code="(.)"/ && index($cost_order,$1) },
             );

     cmpthese( shift || -5, {
         'S-Maker' => sub {
             my @sorted = $sorter->( @unsorted );
         },

         'S-Maker-compiling' => sub {

		    my $sorter = make_sorter(
               'GRT',
               init_code   => "my \$cost_order = '$cost_order';",
               signed      => 1,
               string_data => 1,
               number      => q{ /code="(.)"/ && index($cost_order,$1) },
             );
             my @sorted = $sorter->( @unsorted );
         },
         'grep()' => sub {
             my $co = $cost_order;
             my @sorted = ();
             while ( my $mc = chop $co ) {
                 unshift @sorted, grep /code="$mc"/, @unsorted;
             }
         },
     } );
