in reply to Re^2: An efficient way to gather a common portion of several strings' beginnings
in thread An efficient way to gather a common portion of several strings' beginnings

Maybe of interest:

I tried to beat sort, where much of the result is useless, cause we only need the first and last element and nothing in between.

The results are not too spectacular, though its requires less memory:

use warnings; use strict; use Time::HiRes qw/time/; my $common = join "","a".."z"; my @x = map { $common . rand 1 } 1..1e6; my (@a,$a,$b,$start); print "\n--- with full sort\n"; @a=@x; $start=time(); my @b= sort @a; print $b[0],"\n",$b[-1],"\n"; print time -$start,"\n"; print "\n--- with triple sort\n"; @a=@x; $start=time(); $a= shift @a; $b= shift @a; ($a,undef,$b) = sort($a,$_,$b) for @a; print $a,"\n",$b,"\n"; print time -$start,"\n"; print "\n--- with assignment\n"; @a=@x; $start=time(); $a= shift @a; $b= shift @a; for my $x (@a) { $a = ($x,$x,$a)[$a cmp $x]; #next if $a eq $x; $b = ($x,$b,$x)[$b cmp $x]; } print $a,"\n",$b,"\n"; print time -$start,"\n"; print "\n--- with goto\n"; @a=@x; $start=time(); $a= shift @a; $b= shift @a; for my $x (@a) { goto ("NEXT", "NEWMIN", "MAYBEMAX")[$a cmp $x]; NEWMIN: $a=$x; next; MAYBEMAX: goto ("NEXT", "NEXT", "NEWMAX" )[$b cmp $x]; NEWMAX: $b=$x; NEXT: } print $a,"\n",$b,"\n"; print time -$start,"\n";

output:

--- with full sort abcdefghijklmnopqrstuvwxyz0.000100396248079448 abcdefghijklmnopqrstuvwxyz9.99150346814304e-06 7.66956496238708 --- with triple sort abcdefghijklmnopqrstuvwxyz0.000100396248079448 abcdefghijklmnopqrstuvwxyz9.99150346814304e-06 5.88848209381104 --- with assignment abcdefghijklmnopqrstuvwxyz0.000100396248079448 abcdefghijklmnopqrstuvwxyz9.99150346814304e-06 1.2906858921051 --- with goto abcdefghijklmnopqrstuvwxyz0.000100396248079448 abcdefghijklmnopqrstuvwxyz9.99150346814304e-06 2.99558115005493

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

update

either I'm working too hard or Alzheimer is knocking at the door.

I completely forgot about le and ge and spend too much effort into emulation

print "\n--- with le/ge\n"; @a=@x; $start=time(); $a= shift @a; $b= shift @a; for my $x (@a) { $a = $x if $a ge $x; $b = $x if $b le $x; } print $a,"\n",$b,"\n"; print time -$start,"\n"; __END__ --- with le/ge abcdefghijklmnopqrstuvwxyz0.000100420329498974 abcdefghijklmnopqrstuvwxyz9.95282924378671e-05 0.773550033569336