
Untitled
By: a guest on
Apr 4th, 2012 | syntax:
None | size: 1.12 KB | hits: 14 | expires: Never
use common::sense;
use Benchmark ':all';
package OverloadItr {
use overload
q{<>} => sub { $_[0]->next };
sub new {
bless {
count => 10000,
}, shift;
}
sub next {
my $count = $_[0]->{count}--;
return unless $count;
return $count;
}
}
package OverloadItrWithGoto {
use overload
q{<>} => sub { goto &next };
sub new {
bless {
count => 10000,
}, shift;
}
sub next {
my $count = $_[0]->{count}--;
return unless $count;
return $count;
}
}
say $^V;
cmpthese(100, {
next => sub {
my $itr = OverloadItr->new;
while ($itr->next) {}
},
itr => sub {
my $itr = OverloadItr->new;
while (<$itr>) {}
},
itr_goto => sub {
my $itr = OverloadItrWithGoto->new;
while (<$itr>) {}
},
});
__END__
v5.13.7
(warning: too few iterations for a reliable count)
Rate itr itr_goto next
itr 104/s -- -2% -42%
itr_goto 106/s 2% -- -40%
next 179/s 71% 68% --