in reply to Re^2: Macro in perl code?
in thread Macro in perl code?

Adding some overhead in the subroutine being called, the difference (if any) between the two approaches disappears altogether:
$ cat 645237.pl use strict; use warnings; use Benchmark qw/ cmpthese /; use List::Util qw/ sum /; sub foo { my ($volume, $message) = @_; my @foo = ($volume .. $message); @foo = map { $_ ** 2 } @foo; return sum(@foo); } sub bar { my $volume = shift; my $message = shift; my @foo = ($volume .. $message); @foo = map { $_ ** 2 } @foo; return sum(@foo); } cmpthese(100_000, { shift => sub { foo(1, 100) }, inline => sub { bar(1, 100) }, } ); $ perl 645237.pl Rate inline shift inline 8396/s -- -0% shift 8403/s 0% --
If your Benchmark shows your computer is able to perform an operation 3 million times pr. second, that's equivalent of a 1000 m2 highway sign saying: Don't optimize!

Update: Changed code to supply arguments to functions as pointed out by bruceb3.

--
Andreas

Replies are listed 'Best First'.
Re^4: Macro in perl code?
by bruceb3 (Pilgrim) on Oct 16, 2007 at 21:27 UTC
    What is interesting is that the code that you benchmarked and the code that I benchmarked, doesn't actually supply arguments to the either function, which really is the point after all.

    Changing the cmpthese call to;

    cmpthese(100_000, { shift => sub { foo(1, "foo") }, inline => sub { bar(1, "bar") } });

    The results are the same for either method;

    bruce:0:~/tmp $ ./535237.pl Rate shift inline shift 15456/s -- -0% inline 15528/s 0% --