I tried:
compared to:sub ext_s { # Returns characters in sorted order my ($s) = @_ ; my %h ; @h{split(//, $s)} = undef ; return join('', sort keys %h) ; } ; sub ext_o { # Returns characters in original order my ($s) = @_ ; my @h ; return join('', grep { !$h[ord($_)]++ } split(//, $s)) ; } ;
and benchmarked:sub ext_dw { my ($s) = @_ ; my %h ; $h{$_} = undef foreach split(//, $s) ; return join('', sort keys %h) ; } ; sub ext_cn { my ($s) = @_ ; my %h ; $s =~ s/(.)/$h{$1}++?'':$1/ge ; return $s ; } ;
Rate cn dw s o
cn 10101/s -- -40% -54% -59%
dw 16949/s 68% -- -22% -31%
s 21739/s 115% 28% -- -11%
o 24390/s 141% 44% 12% --
In reply to Re: Collapsing a string to unique characters
by gone2015
in thread Collapsing a string to unique characters
by dwhite20899
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |