#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Benchmark::Forking qw( cmpthese ); my %hash = ( "a" => 1, "b" => 2, "c" => 3, "d" => 4 ); # Export keys from the hash my @keys = keys %hash; # Export values from the hash my @values = values %hash; my %new_hash_1; my %new_hash_2; cmpthese(100000000, { 'loop indices' => sub { for ( 0 .. $#keys ) { $new_hash_1{splice @keys, rand @keys, 1} = splice @values, rand @values, 1; } }, 'loop elements' => sub { for ( @keys ) { $new_hash_2{splice @keys, rand @keys, 1} = splice @values, rand @values, 1; } }, }); __DATA__ $ perl test.pl Rate loop indices loop elements loop indices 5211047/s -- -58% loop elements 12531328/s 140%