in reply to Collapsing a string to unique characters
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% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Collapsing a string to unique characters
by dwhite20899 (Friar) on Jan 09, 2009 at 20:00 UTC | |
by BrowserUk (Patriarch) on Jan 09, 2009 at 20:42 UTC |