Thread Eindeutige Liste aller Zeichen im String
(32 answers)
Opened by roli at 2008-03-11 18:39
and the winner is...
Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 sub hash1 { my %seen; $seen{$_}++ for ( split( //, $string ) ); my $res = join( '', sort keys %seen ); return $res; } sub hash2 { my $res = $string; my %seen; $res =~ s/(.)/$seen{$1}++ ? "" : $1/gse; return $res; } sub joinsplit1 { my $res = join '', split /(.)\1+/, join('', sort( split('', $string ))); } sub hash3 { my $res = join "", sort keys %{ { map{ $_ => 1 } split //, $string } }; } sub join_tr { my $res = join("",sort(split("",$string))); $res =~ tr/0-9a-zA-Z//s; return $res; } use Benchmark; timethese($ARGV[0] || 100, { hash1 => \&hash1, hash2 => \&hash2, hash3 => \&hash3, joinsplit1 => \&joinsplit1, join_tr => \&join_tr, }); Code: (dl
)
1 Benchmark: timing 40000 iterations of hash1, hash2, hash3, join_tr, joinsplit1... Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: ![]() ![]() |