On Dec 30, 2007 5:49 PM, tom arnall <[EMAIL PROTECTED]> wrote:
> oops. i just ran the code. no diff' when run $TWO_THIRDS. oh well, in Perl
> subroutines are cheap!

Subroutines aren't that cheap, but making them unary operators lessens
the costs and using the constant pragma makes it even cheaper:

CONSTANT => 10
func => 10
literal => 10
unary => 10
var => 10
               Rate     func      var    unary CONSTANT  literal
func      2595485/s       --     -82%     -95%     -97%     -97%
var      14534717/s     460%       --     -74%     -84%     -85%
unary    55361213/s    2033%     281%       --     -39%     -44%
CONSTANT 91039156/s    3408%     526%      64%       --      -8%
literal  98576846/s    3698%     578%      78%       8%       --

#!/usr/local/ActivePerl-5.10/bin/perl

use strict;
use warnings;
use feature ':5.10';
use Benchmark;

use constant CONSTANT => 5;

sub unary () { 5 }
sub func { 5 }
my $var = 5;

my %subs = (
        literal   => sub { 5 + 5 },
        CONSTANT  => sub { CONSTANT + 5 },
        unary     => sub { unary + 5 },
        func      => sub { func() + 5 },
        var       => sub { $var + 5 },
);

say "$_ => ", $subs{$_}() for sort keys %subs;

Benchmark::cmpthese(-1, \%subs);

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to