$ cat pm_hash_clear_vs_undef_1214851.pl use strict; use warnings; use Benchmark ':all'; use Hash::Util 'bucket_stats'; my @some_keys = ('A' .. 'Z', 'a' .. 'z', '0' .. '9'); print "Checking container differences between delete and clear:\n"; my %gh; print "Hash size (initial): ", old_hash_stats(\%gh), "\n"; fill_hash(\%gh); print "Hash size (after fill): ", old_hash_stats(\%gh), "\n"; %gh=(); print "Hash size (after clear): ", old_hash_stats(\%gh), "\n"; undef %gh; print "Hash size (after delete): ", old_hash_stats(\%gh), "\n"; print "\n"; sub old_hash_stats { my $hr = shift; my @hash_stats = bucket_stats($hr); return "$hash_stats[0]/$hash_stats[1]"; } sub fill_hash { my $hr = shift; @{$hr}{@some_keys} = (0) x @some_keys; } cmpthese(500000, { 'overwrite' => sub { my %h; fill_hash(\%h); fill_hash(\%h); }, 'delete' => sub { my %h; fill_hash(\%h); undef %h; fill_hash(\%h); }, 'reuse' => sub { my %h; fill_hash(\%h); %h = (); fill_hash(\%h); }, });