in reply to Re: Re: Re: Re: Optimizing a string processing sub
in thread Optimizing a string processing sub

Very good catch, MarkM!. I didn't really thoroughly test my solution, leaving that as an exercise for the reader.

As for my solution, although this wasn't in the original specs, I also wanted to solve the problem without using some variant of split (which /(.)/g is). However, I'm blanking on it.

Although this may have been posted in the mainline discussion, I'll post this, as it Benchmark's faster than yours.

sub score { my ($x, $y) = @_; return -1 if $x eq $y; my %x; $x{$_}++ for $x =~ /./g; my $score = 0; $x{$_} && $score++ for $y =~/./g; $score; }
For 1 million iterations, this benches at 74.56sec. Yours benches at 122.51sec. :-)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.