in reply to Re: noncase-sensitive sorting
in thread noncase-sensitive sorting
And the output is -use strict; use Data::Dumper; use Benchmark qw/timethese cmpthese/; my @letters = 'A'..'Z'; my @array; # generate array of 1000 random length random upper/lower # case strings to feed into the test subs. foreach (0..1000) { my $c = $letters [rand 26]; $c = int(rand 2) ? uc $c : lc $c; push @array, $c x (1 + rand 10); } cmpthese( timethese(1000, { 'Schwartzian' => '&sort_schwartzian', 'Simple' => '&sort_simple', }) ); sub sort_schwartzian { my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, uc $_ ] } @array; } sub sort_simple { my @sorted = sort { uc($a) cmp uc($b) } @array; }
It shows that the simple sort is much faster than the Schwartzian sort, perhaps because of all the overheads the Schwartzian sort has. And it demonstrates that the perl built-in uc and lc functions are quite efficient. %^pBenchmark: timing 1000 iterations of Schwartzian, Simple... Schwartzian: 16 wallclock secs (16.39 CPU) @ 61.01/s (n=1000) Simple: 11 wallclock secs (10.95 CPU) @ 91.32/s (n=1000) Rate Schwartzian Simple Schwartzian 61.0/s -- -33% Simple 91.3/s 50% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: noncase-sensitive sorting
by sauoq (Abbot) on Dec 02, 2003 at 11:08 UTC | |
|
Re: Re: Re: noncase-sensitive sorting
by davido (Cardinal) on Dec 02, 2003 at 07:16 UTC | |
by Roger (Parson) on Dec 02, 2003 at 07:25 UTC | |
by Abigail-II (Bishop) on Dec 02, 2003 at 10:48 UTC | |
by Roger (Parson) on Dec 02, 2003 at 11:34 UTC | |
by Abigail-II (Bishop) on Dec 02, 2003 at 12:46 UTC |