Skip to content

Instantly share code, notes, and snippets.

@petdance
Last active January 10, 2023 21:15
Show Gist options
  • Save petdance/1c8ea0570dca5f60fcd860b69a2b4fc6 to your computer and use it in GitHub Desktop.
Save petdance/1c8ea0570dca5f60fcd860b69a2b4fc6 to your computer and use it in GitHub Desktop.
Benchmarking /literal/ vs /$foo/ vs /$foo/o
$ cat slash-o.pl
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use Benchmark ':all';
my $COUNT = 10_000_000;
say "$COUNT iterations under $^V";
my $str = 'Who knows what evil lurks in the hearts of men?';
my $pattern = 'in\s+the';
cmpthese( $COUNT, {
noninterpolated => sub {
return $str =~ /lurks in\s+the/;
},
interpolated => sub {
return $str =~ /lurks $pattern/;
},
slasho => sub {
return $str =~ /lurks $pattern/o;
}
} );
exit 0;
$ perl slash-o.pl
10000000 iterations under v5.20.3
Rate interpolated noninterpolated slasho
interpolated 2070393/s -- -56% -56%
noninterpolated 4694836/s 127% -- -0%
slasho 4716981/s 128% 0% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment