Skip to content

Instantly share code, notes, and snippets.

@ken39arg
Created November 11, 2014 06:36
Show Gist options
  • Select an option

  • Save ken39arg/d779f2fa54a85e8050f8 to your computer and use it in GitHub Desktop.

Select an option

Save ken39arg/d779f2fa54a85e8050f8 to your computer and use it in GitHub Desktop.
前後空白トリム
use common::sense;
use Benchmark qw(:all);
my @words = (
' ほげ ',
'ほげ ',
' ぶう',
'ぶう',
'あいうえおかきくけっこ ',
' あいうえおかきくけっこ',
);
my $results = timethese( 100000, {
onestep => sub {
my $data = $words[int(rand(6))];
$data =~ s/^\s*(.+?)\s*$/$1/;
},
twostep => sub {
my $data = $words[int(rand(6))];
$data =~ s/^\s+//;
$data =~ s/\s+$//;
},
} );
cmpthese( $results );
__DATA__
Benchmark: timing 100000 iterations of onestep, twostep...
onestep: 1 wallclock secs ( 0.38 usr + 0.00 sys = 0.38 CPU) @ 263157.89/s (n=100000)
(warning: too few iterations for a reliable count)
twostep: 0 wallclock secs ( 0.18 usr + 0.00 sys = 0.18 CPU) @ 555555.56/s (n=100000)
(warning: too few iterations for a reliable count)
Rate onestep twostep
onestep 263158/s -- -53%
twostep 555556/s 111% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment