#!/usr/bin/perl use Benchmark qw(:all :hireswallclock); $count = 1000; for(1..$count){ $hash{$_} = $_; } timethese($count, { 'each' => sub { while (my ($key, $value) = each %hash) { $hash{$key} = 1; } }, 'foreach keys' => sub { foreach (keys %hash) { $hash{$_} = 1; } }, 'for keys' => sub { for (keys %hash) { $hash{$_} = 1; } }, } );