#! perl -slw use strict; use Time::HiRes qw[ time ]; { package Tied::Hash; sub TIEHASH { return bless {}, $_[0]; } sub STORE{ $_[0]->{ $_[1] } = $_[2] } sub FETCH{ $_[0]->{ $_[1] } } sub FIRSTKEY{ scalar keys %{ $_[0] }; each %{ $_[0] }; } sub NEXTKEY{ each %{ $_[0] }; } sub EXISTS{ exists $_[0]->{ $_[1] } } sub DELETE{ delete $_[0]->{ $_[1] } } sub CLEAR{ undef %{ $_[0] } } sub SCALAR{ scalar %{ $_[0] } } } use constant{ FIRST => 'aaaaa', LAST => 'zzzzz' }; my $start = time; { my %hash; $hash{ $_ } = $_ for FIRST .. LAST; print scalar %hash; while( my $key = each %hash ) { $hash{ $key } = unpack 'V', $hash{ $key } } delete $hash{ $_ } for FIRST .. LAST; undef %hash; } printf "std hash usage took: %.3f seconds\n", time() - $start; $start = time; { tie my %hash, 'Tied::Hash'; $hash{ $_ } = $_ for FIRST .. LAST; print scalar %hash; while( my $key = each %hash ) { $hash{ $key } = unpack 'V', $hash{ $key } } delete $hash{ $_ } for FIRST .. LAST; undef %hash; } printf "tied usage took: %.3f seconds\n", time() - $start; $start = time; { my $o = tie my %hash, 'Tied::Hash'; $o->STORE( $_, $_ ) for FIRST .. LAST; print $o->SCALAR; my $key = $o->FIRSTKEY(); while( $key = $o->NEXTKEY( $key ) ) { $o->STORE( $key, unpack 'V', $o->FETCH( $key ) ); } $o->DELETE( $_ ) for FIRST .. LAST; $o->CLEAR(); $o->UNTIE; } printf "Object usage took: %.3f seconds\n", time() - $start; #### __END__ C:\test>b-tieHash.pl 8376437/16777216 std hash usage took: 56.582 seconds 8376437/16777216 tied usage took: 210.352 seconds 8376437/16777216 Object usage took: 126.642 seconds