in reply to benchmark for shift vs list assignment for object methods

Your benchmark is interesting, however, sadly, completely useless.

perldoc -f shift:

shift   Shifts the first value of the array off and returns it
        shortening the array by 1 and moving everything down.
You see that, shift modifies the array (as i'm sure you already know). Thus, the benchmark is void.

On another note, the danger of using @_ directly is the same as that of doing practically anything in perl (there's plenty of rope to hang yourself).

I ++ you symbolically for experimenting, and satisfying your curiosity (with code ;).

update: an interesting read, related to the subject of benchmarking and "efficiency", is Premature optimization by Curtis POE

update: Also, I like cmpthese so much more for benchmarks, in my version of Benchmark.pm, it's exported by default ;) (see below for code)

update: Another monk raised the question of why I think this benchmark is, well, pointelss. Is think it's like comparing apples and oranges. Since oranges do more work, and do different things than apples, why would you compare them?

#!/usr/bin/perl -w use Benchmark qw( cmpthese ); # for sake of others ;) use strict; package ArgTest; sub shifty_1arg { my $this = shift; my $first_arg = shift; $$this += $first_arg; } sub shifty_3args { my $this = shift; my $first_arg = shift; my $second_arg = shift; my $third_arg = shift; $$this += $first_arg + $second_arg + $third_arg; } sub argumentative_1arg { my( $this, $first_arg ) = @_; $$this += $first_arg; } sub argumentative_3args { my( $this, $first_arg, $second_arg, $third_arg ) = @_; $$this += $first_arg + $second_arg + $third_arg; } sub direct_1arg { ${$_[0]} += $_[1]; } sub direct_3args { ${$_[0]} += $_[1] + $_[2] + $_[3]; } # main /------------------------------------------------ package main; my $total; bless( my $object = \$total, 'ArgTest' ); my @args = ( 1 .. 3 ); cmpthese( 1_000_000, { shifty_1arg => sub { $object->shifty_1arg( @args ) }, argumentative_1arg => sub { $object->argumentative_1arg( @ar +gs ) }, direct_1arg => sub { $object->direct_1arg( @args ) }, shifty_3args => sub { $object->shifty_3args( @args ) } +, argumentative_3args => sub { $object->argumentative_3args( @a +rgs ) }, direct_3args => sub { $object->direct_3args( @args ) } +, } ); print "total: $total\n"; __END__ Benchmark: timing 1000000 iterations of argumentative_1arg, argumentat +ive_3args, direct_1arg, direct_3args, shifty_1arg, shifty_3args... argumentative_1arg: 1 wallclock secs ( 1.36 usr + 0.00 sys = 1.36 C +PU) @ 735835.17/s (n=1000000) argumentative_3args: 3 wallclock secs ( 1.80 usr + 0.00 sys = 1.80 +CPU) @ 556173.53/s (n=1000000) direct_1arg: 0 wallclock secs ( 1.00 usr + 0.00 sys = 1.00 CPU) @ 1 +000000.00/s (n=1000000) direct_3args: 1 wallclock secs ( 1.19 usr + 0.00 sys = 1.19 CPU) @ +841750.84/s (n=1000000) shifty_1arg: 3 wallclock secs ( 1.59 usr + 0.00 sys = 1.59 CPU) @ 6 +27746.39/s (n=1000000) shifty_3args: 3 wallclock secs ( 2.34 usr + 0.00 sys = 2.34 CPU) @ +426621.16/s (n=1000000) Rate shifty_3args
argumentative_3args shifty_1arg argumentative_1arg direct_3args direct_1arg
shifty_3args         426621/s           --                -23%        -32%               -42%         -49%        -57%
argumentative_3args  556174/s          30%                  --        -11%               -24%         -34%        -44%
shifty_1arg          627746/s          47%                 13%          --               -15%         -25%        -37%
argumentative_1arg   735835/s          72%                 32%         17%                 --         -13%        -26%
direct_3args         841751/s          97%                 51%         34%                14%           --        -16%
direct_1arg         1000000/s         134%                 80%         59%                36%          19%          --
total: 21000000

____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: benchmark for shift vs list assignment for object methods
by thpfft (Chaplain) on Jul 28, 2002 at 18:54 UTC

    I fail to see why this bit of exploration deserves to be described as useless, pointless or void. it takes a few idioms often used for the same purpose, each with surprisingly determined adherents, and tries to find out which is quickest. The results seem valid and well presented, if not very startling. What's the problem?

    If your point is that these approaches have very different side effects and that people in the habit of using (...) = @_ and &func are going to need to know that, then I quite agree, but it doesn't invalidate the benchmark.

    If the problem is in the methodology then perhaps you could be a little more specific, for the benefit of people like me who cannot see?

Re: benchmark for shift vs list assignment for object methods
by Abigail-II (Bishop) on Jul 29, 2002 at 10:16 UTC
    shift is indeed modifying the array, but keep in mind this is the @_ array, not a globally used one. It does make a difference if you later in your subroutine would use @_, but that doesn't happen that often.

    List assignment and shifting off from @_ are two very commonly used techniques to put the subroutine arguments into variables. And since @_ is not used further in the subroutine, the fact that @_ is or isn't modified is irrelevant.

    Abigail

Re: Re: benchmark for shift vs list assignment for object methods
by belg4mit (Prior) on Jul 29, 2002 at 00:04 UTC
    local @_ =  @_; #UPDATE added assignment

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Re: Re: benchmark for shift vs list assignment for object methods
by crenz (Priest) on Aug 02, 2002 at 07:29 UTC

    >You see that, shift modifies the array (as i'm sure you already know). Thus, the benchmark is void.

    Huh? Where is the problem? You can modify test internal data as much as you want, as long as it is initialized the same way for each test. It is no problem to modify @_ here. There would be a problem if he would directly modify the global @args. Which he doesn't do. Or are you trying to indicate that

    my @arr = ('a', 'b', 'c'); sub dosth { my $first = shift; } dosth(@arr); print "@arr";

    results in "a b"? Of course not, right?