if (not $P::x) { $P::x = 1; f(); } #### use strict; use warnings; use Benchmark qw( timethese cmpthese ); my $results = timethese( 5000, { orig => sub { delete $P::x{some_key}; for ( 1 .. 1000 ) { $P::x{some_key} ||= ( f(), 1 ); } }, alt_1 => sub { delete $P::x{some_key}; for ( 1 .. 1000 ) { $P::x{some_key} = $P::x{some_key} || ( f(), 1 ); } }, alt_2 => sub { delete $P::x{some_key}; for ( 1 .. 1000 ) { if ( not $P::x{some_key} ) { $P::x{some_key} = 1; f(); } } }, } ); cmpthese($results); sub f() { #No op. } #### Benchmark: timing 5000 iterations of alt_1, alt_2, orig... alt_1: 3 wallclock secs ( 3.08 usr + 0.00 sys = 3.08 CPU) @ 1623.38/s (n=5000) alt_2: 2 wallclock secs ( 1.29 usr + 0.00 sys = 1.29 CPU) @ 3875.97/s (n=5000) orig: 1 wallclock secs ( 1.25 usr + 0.00 sys = 1.25 CPU) @ 4000.00/s (n=5000) Rate alt_1 alt_2 orig alt_1 1623/s -- -58% -59% alt_2 3876/s 139% -- -3% orig 4000/s 146% 3% --