Re: is this a rigged test?



On Dec 30, 2007 5:49 PM, tom arnall <kloro2006@xxxxxxxxx> 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);
.



Relevant Pages

  • Save as another workspace
    ... I have a program with a lot of functions & subroutines. ... and add all the func. ... & subs to the project. ... H. Liu ...
    (comp.lang.fortran)
  • use strict and local variables
    ... is there a possibility to use "use strict" and to define variables in ... which are also seen in the subroutines called from this ... I want the same value in the var in the subroutine like before, ...
    (perl.beginners)
  • Simulate Mouse Event
    ... var x = 0; ... how to simulate or avoid using "ev" variable in runMe() but ... places and subroutines, so passing event seems to be a complicated ...
    (comp.lang.javascript)
  • is this a rigged test?
    ... no diff' when run $TWO_THIRDS. ... oh well, in Perl ... subroutines are cheap! ...
    (perl.beginners)