in reply to benchmark for shift vs list assignment for object methods
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 | |
Re: benchmark for shift vs list assignment for object methods
by Abigail-II (Bishop) on Jul 29, 2002 at 10:16 UTC | |
Re: Re: benchmark for shift vs list assignment for object methods
by belg4mit (Prior) on Jul 29, 2002 at 00:04 UTC | |
Re: Re: benchmark for shift vs list assignment for object methods
by crenz (Priest) on Aug 02, 2002 at 07:29 UTC |