in reply to Re^2: TIMTOWTDI - printing an array
in thread TIMTOWTDI - printing an array
I think this benchmark code confirms my point regarding the expense of continually creating and invoking anonymous subroutines.
use strict; use warnings; use Benchmark qw{ cmpthese }; my @arr = ( 1 .. 1e6 ); my $rcOneElement = sub { my $aref = shift; sub { my $var = shift }->( $_ ) for @$aref; }; my $rcAllElements = sub { my $aref = shift; sub { my $var = shift while @_ }->( @$aref ); }; cmpthese ( -5, { OneElement => sub { $rcOneElement->( \ @arr ) }, AllElements => sub { $rcAllElements->( \ @arr ) }, } );
The output.
Rate OneElement AllElements OneElement 2.59/s -- -58% AllElements 6.16/s 138% --
However, I am good at cocking up benchmarks so I could be deluding myself :-/
Cheers,
JohnGG
|
|---|