Skip to content

Instantly share code, notes, and snippets.

@schwern
Last active August 29, 2015 14:15
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 schwern/b4cc353399abae1c7f5a to your computer and use it in GitHub Desktop.
Save schwern/b4cc353399abae1c7f5a to your computer and use it in GitHub Desktop.
Overhead of a wrapper function in Perl
use strict;
use warnings;
use Benchmark qw(cmpthese);
sub match { $_[0] =~ $_[1] }
sub wrapper { match(@_) }
sub wrapper_alias_args { &match }
my @args = ("foo", qr/bar/);
cmpthese -3, {
match => sub { match(@args) },
wrapper => sub { wrapper(@args) },
wrapper_alias_args => sub { wrapper_alias_args(@args) },
};
__END__
Rate wrapper wrapper_alias_args match
wrapper 1124156/s -- -6% -18%
wrapper_alias_args 1199177/s 7% -- -12%
match 1363269/s 21% 14% --
@schwern
Copy link
Author

schwern commented Feb 20, 2015

Wow, changing $args[1] from a string to a regex chops performance in half. Weird. I guess it's more expensive to copy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment