| 1 | #!perl | ||
| 2 | use strict; | ||
| 3 | use ExtUtils::testlib; | ||
| 4 | |||
| 5 | use Benchmark qw( cmpthese ); | ||
| 6 | |||
| 7 | ######################################################################## | ||
| 8 | |||
| 9 | my @generators = qw( Basic Standard Composite Evaled Template ); | ||
| 10 | my @methods = map "method_$_", 1 .. 30; | ||
| 11 | |||
| 12 | my %pckgno; | ||
| 13 | my $cputime = shift(@ARGV) || 0; | ||
| 14 | |||
| 15 | ######################################################################## | ||
| 16 | |||
| 17 | print "\nComparing module load time (usage overhead)...\n"; | ||
| 18 | |||
| 19 | my %inc = %INC; | ||
| 20 | sub test_load { | ||
| 21 | local %INC = %inc; | ||
| 22 | my $f = "$_[0].pm"; | ||
| 23 | $f =~ s{::}{/}g; | ||
| 24 | require $f; | ||
| 25 | } | ||
| 26 | |||
| 27 | cmpthese( $cputime, { | ||
| 28 | 'Superclass' => sub { | ||
| 29 | test_load( "Class::MakeMethods" ) | ||
| 30 | }, | ||
| 31 | map { | ||
| 32 | my $gen = $_; | ||
| 33 | $gen => sub { | ||
| 34 | test_load( "Class::MakeMethods::${gen}::Hash" ) | ||
| 35 | } | ||
| 36 | } @generators | ||
| 37 | } ); | ||
| 38 | |||
| 39 | ######################################################################## | ||
| 40 | |||
| 41 | print "\nComparing method generation (startup duration)...\n"; | ||
| 42 | |||
| 43 | cmpthese( $cputime, { | ||
| 44 | 'inline' => sub { | ||
| 45 | eval( join "\n", | ||
| 46 | "package package_inline_" . ++ $pckgno{inline} . ";", | ||
| 47 | 'sub new { my $class = shift; bless { @_ }, $class }', | ||
| 48 | map "sub $_ { my \$self = shift; \@_ ? \$self->{$_} = shift : \$self->{$_} }", @methods | ||
| 49 | ); | ||
| 50 | }, | ||
| 51 | map { | ||
| 52 | my $gen = $_; | ||
| 53 | $gen => sub { | ||
| 54 | Class::MakeMethods->make( | ||
| 55 | -MakerClass => $gen . "::Hash", | ||
| 56 | -TargetClass => ( "package_${gen}_" . ++ $pckgno{$gen} ), | ||
| 57 | 'new' => 'new', | ||
| 58 | 'scalar' => \@methods | ||
| 59 | ); | ||
| 60 | } | ||
| 61 | } @generators | ||
| 62 | } ); | ||
| 63 | |||
| 64 | ######################################################################## | ||
| 65 | |||
| 66 | print "\nComparing method calling (runtime duration)...\n"; | ||
| 67 | |||
| 68 | cmpthese( $cputime, { | ||
| 69 | map { | ||
| 70 | my $gen = $_; | ||
| 71 | $gen => sub { | ||
| 72 | my $instance = "package_${gen}_1"->new(); | ||
| 73 | foreach ( 1 .. 5 ) { | ||
| 74 | foreach my $method ( @methods ) { | ||
| 75 | my $value = $instance->$method(); | ||
| 76 | $instance->$method( $value ); | ||
| 77 | $instance->$method(); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } keys %pckgno | ||
| 82 | } ); | ||
| 83 | |||
| 84 | ######################################################################## | ||
| 85 | |||
| 86 | __END__ |