Skip to content

Instantly share code, notes, and snippets.

@vitaminmoo
Created January 25, 2012 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitaminmoo/1674789 to your computer and use it in GitHub Desktop.
Save vitaminmoo/1674789 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use Benchmark qw(:all);
$a = "foo[bar]moo";
cmpthese(
10000000, {
capture => sub {
$a =~ /\[(\w)\]/;
$1;
},
zerowidth => sub {
$a =~ /(?<=\[)(\d+)(?=\])/;
$1;
},
split => sub{
$b = (split( /\[|\]/, $a))[1];
$b;
},
twosplit => sub{
$b = (split( /\[/, (split( /\]/, $a ))[0]))[1];
$b;
},
}
);
__END__
Rate split twosplit capture zerowidth
split 233209/s -- -58% -84% -85%
twosplit 559597/s 140% -- -61% -64%
capture 1428571/s 513% 155% -- -7%
zerowidth 1536098/s 559% 175% 8% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment