Skip to content

Instantly share code, notes, and snippets.

@azumakuniyuki
Created May 8, 2022 05:55
Show Gist options
  • Save azumakuniyuki/27648af3596c00a65183e7ac5a6d1608 to your computer and use it in GitHub Desktop.
Save azumakuniyuki/27648af3596c00a65183e7ac5a6d1608 to your computer and use it in GitHub Desktop.
s///, s//// for $v
#!/usr/bin/env perl
# Captured $1 vs. ${^MATCH}
use strict;
use warnings;
use Benchmark ':all';
use Test::More 'no_plan';
my $q = ' neko nyaan cat ';
sub s3 {
my $v = shift;
$v =~ s/\A //;
$v =~ s/ \z//;
$v =~ s/ /-/g;
return $v;
}
sub s4 {
my $v = shift;
s/\A //, s/ \z//, s/ /-/g for $v;
return $v;
}
is s3($q), 'neko-nyaan-cat';
is s4($q), 'neko-nyaan-cat';
printf("Running with Perl %s on %s\n%s\n", $^V, $^O, '-' x 80);
cmpthese(6e6, {
's/...// x 3' => sub { s3($q) },
's/...// for' => sub { s4($q) },
}
);
__END__
Running with Perl v5.30.0 on darwin
--------------------------------------------------------------------------------
Rate s/...// x 3 s/...// for
s/...// x 3 454201/s -- -6%
s/...// for 483092/s 6% --
Running with Perl v5.32.0 on darwin
--------------------------------------------------------------------------------
Rate s/...// x 3 s/...// for
s/...// x 3 456274/s -- -3%
s/...// for 469116/s 3% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment