#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); our @array; sub pdcawley { my @copy = @array; while (my @chunk = splice @copy, 0, 2) { } } sub juerd { my $refs = sub { \@_ }->(@array); while (my @chunk = splice @$refs, 0, 2) { } } sub bench { printf "\n\e[1m%s\e[0m\n", shift; cmpthese(-10, { pdcawley => \&pdcawley, juerd => \&juerd }); } @array = (1) x 32767; bench "Long array, tiny values"; @array = ("x" x 32) x 32767; bench "Long array, small values"; @array = (1) x 32; bench "Short array, tiny values"; @array = ("x" x 32) x 32; bench "Short array, small values"; @array = ("x" x (2**20)) x 32; bench "Short array, large values"; @array = ("x" x (8 * 2**20)) x 32; bench "Short array, huge values";