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% --
In reply to Re: Re: noncase-sensitive sorting
by Roger
in thread noncase-sensitive sorting
by texuser74
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |