Perl shift iterate Benchmark

This little Perl Benchmark shows that its better to iterate through an array as to manipulate via shift.For more about shift look at Perl array pop, push, shift, unshift.

use strict;
use warnings;
use Benchmark qw(:all) ;

cmpthese(-1, {
'shift'   => sub {my @var = (1..4);print shift(@var); },
'iterate' => sub {my @var = (1..4);print $var[0]; },

We see its better to iterate as to shift from an array:

shift   1147836/s      --     -4%  
iterate 1191563/s      4%      --

4 thoughts on “Perl shift iterate Benchmark”

  1. Outstanding post however , I was wanting to know if you could write
    a litte more on this topic? I’d be very thankful if you could elaborate a
    little bit further. Kudos!

  2. It’s impressive that you are getting ideas from this article as well as from our discussion made here.

  3. For most recent information you have to pay a quick visit world-wide-web and on the web I found this website as
    a most excellent website for hottest updates.

Leave a Reply

Your email address will not be published.

three × 5 =