#!/usr/bin/env perl -l
use strict;
use warnings;
use utf8;
use open OUT => ':utf8', ':std';
my @strings = (
'A B C', 'D E', 'F ', 'G', 'H ', 'I ',
"\N{MERCURY} \N{FEMALE SIGN} \N{EARTH}", "\N{MALE SIGN} \N{JUPITER}",
"\N{SATURN} ", "\N{URANUS}", "\N{NEPTUNE} ", "\N{PLUTO} ",
);
for (@strings) {
next if substr($_, -1, 1) eq ' ' or rindex($_, ' ', length() - 2) == -1;
print;
}
####
#!/usr/bin/env perl -l
use strict;
use warnings;
use Benchmark qw{cmpthese};
my @strings = (
'A B C', 'D E', 'F ', 'G', 'H ', 'I ',
"\N{MERCURY} \N{FEMALE SIGN} \N{EARTH}", "\N{MALE SIGN} \N{JUPITER}",
"\N{SATURN} ", "\N{URANUS}", "\N{NEPTUNE} ", "\N{PLUTO} ",
);
my $re = qr{\s+\b};
cmpthese -1 => {
no_re_check_and_split => \&no_re_check_and_split,
re_check_and_split => \&re_check_and_split,
split_and_re_check => \&split_and_re_check,
};
sub no_re_check_and_split {
for (@strings) {
next if substr($_, -1, 1) eq ' ' or rindex($_, ' ', length() - 2) == -1;
my @parts = split /$re/;
}
}
sub re_check_and_split {
for (@strings) {
next unless /$re/;
my @parts = split /$re/;
}
}
sub split_and_re_check {
for (@strings) {
my @parts = split /$re/;
next if @parts > 1;
}
}
####
Rate split_and_re_check re_check_and_split no_re_check_and_split
split_and_re_check 50243/s -- -18% -29%
re_check_and_split 61265/s 22% -- -13%
no_re_check_and_split 70274/s 40% 15% --
Rate split_and_re_check re_check_and_split no_re_check_and_split
split_and_re_check 53593/s -- -19% -27%
re_check_and_split 66370/s 24% -- -10%
no_re_check_and_split 73770/s 38% 11% --
Rate split_and_re_check re_check_and_split no_re_check_and_split
split_and_re_check 53096/s -- -20% -27%
re_check_and_split 66369/s 25% -- -8%
no_re_check_and_split 72404/s 36% 9% --
ken@ganymede: ~/tmp