#!/usr/bin/env perl use strict; use warnings; use v5.16; use Benchmark qw(cmpthese countit); ############################################################################### ############################################################################### my @coords = qw(1 5 2 6 3 8 6 12 7 5); cmpthese(2000000, { 'one' => sub { one(@coords) }, 'two' => sub { two(@coords) }, 'three' => sub { three(@coords) }, 'four' => sub { four(@coords) }, }); ############################################################################### ############################################################################### sub one { my @coords = @_; for (my $i = 0; $i < @coords; $i += 2) { my $x = $coords[$i]; my $y = $coords[$i+1]; #say STDERR "$x, $y"; } return 1; } sub two { my @coords = @_; while (@coords) { my ($x, $y) = splice(@coords, 0, 2); #say STDERR "$x, $y"; } return 1; } sub three { my @coords = @_; my ($x, $y); while( ($x,$y,@coords) = @coords ) { #say STDERR "$x, $y"; } } sub four { my @coords = @_; while (@coords) { my $x = shift(@coords); my $y = shift(@coords); #say STDERR "$x, $y"; } } __END__ Rate three one two four three 451467/s -- -25% -38% -39% one 598802/s 33% -- -18% -19% two 729927/s 62% 22% -- -1% four 738007/s 63% 23% 1% --