in reply to Re: Re: Re: Re: noncase-sensitive sorting
in thread noncase-sensitive sorting
So uc and lc do become less efficient when the length of the strings to compare are very long.
No, uc and lc don't become 'less efficient'. They just need more time because the strings are longer, while the extra lookup ST does is independent of the length of the string.
Note that a GRT is even faster than an ST:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw/timethese cmpthese/; my @letters = ('A' .. 'Z', 'a' .. 'z'); my $size = 1_000; my $max_l = 1_000; my @array = map {join "" => map {$letters [rand @letters]} 1 .. 1 + ra +nd $max_l} 1 .. $size; my (@st_sorted, @simple_sorted, @grt_sorted); cmpthese -10 => { Schwartzian => \&sort_schwartzian, Simple => \&sort_simple, GRT => \&sort_grt, }; sub sort_schwartzian { @st_sorted = map { $_ -> [0] } sort { $a -> [1] cmp $b -> [1]} map {[$_ => uc $_ ] } @array; } sub sort_simple { @simple_sorted = sort {uc ($a) cmp uc ($b)} @array; } sub sort_grt { @grt_sorted = map {substr $_ => $max_l} sort map {uc () . ("\0" x ($max_l - length)) . $_} @array; } die "Unequal" unless "@st_sorted" eq "@simple_sorted" && "@st_sorted" eq "@grt_sorted"; __END__ Rate Simple Schwartzian GRT Simple 8.99/s -- -78% -81% Schwartzian 40.7/s 353% -- -15% GRT 48.2/s 436% 18% --
Abigail
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: noncase-sensitive sorting
by Roger (Parson) on Dec 02, 2003 at 11:34 UTC | |
by Abigail-II (Bishop) on Dec 02, 2003 at 12:46 UTC |