]> git.vpit.fr Git - perl/modules/with.git/commitdiff
Add the samples/bench.pl script
authorVincent Pit <vince@profvince.com>
Mon, 4 Aug 2008 16:57:21 +0000 (18:57 +0200)
committerVincent Pit <vince@profvince.com>
Mon, 4 Aug 2008 16:57:21 +0000 (18:57 +0200)
samples/bench.pl [new file with mode: 0755]

diff --git a/samples/bench.pl b/samples/bench.pl
new file mode 100755 (executable)
index 0000000..149b964
--- /dev/null
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use Benchmark qw/cmpthese/;
+
+use lib 'blib/lib';
+
+my $counter;
+my @poultry = (qw/cock chick/);
+
+sub Duck::quack { $counter += $_[0]->{id} * $_[0] }
+sub fly { $counter += $_[0]; }
+sub Duck::unshift { CORE::unshift @poultry, $_[0]->{id} * $_[3] }
+sub Duck::pop     { CORE::pop @poultry }
+
+my $duck = bless { id => 0.7 }, 'Duck';
+
+cmpthese -2, {
+ explicit => sub { $duck->quack(3); },
+ with     => sub {
+  use with \$duck;
+  quack -3;
+ }
+};
+
+cmpthese -2, {
+ direct   => sub { fly(2); },
+ deferred => sub {
+  use with \$duck;
+  fly -2;
+ }
+};
+
+cmpthese -2, {
+ core    => sub { push @poultry, 1; shift @poultry; },
+ wrapped => sub {
+  use with \$duck;
+  push @poultry, 1; shift @poultry;
+ }
+};
+
+cmpthese -2, {
+ core      => sub { unshift @poultry, $duck->{id} * 5; pop @poultry; },
+ flattened => sub {
+  use with \$duck;
+  unshift @poultry, -5; pop @poultry;
+ }
+};