Rate anonhash globalhash ternary
anonhash 32323/s -- -59% -63%
globalhash 79642/s 146% -- -8%
ternary 86652/s 168% 9% --
With only a few more entries the global hash would be faster. However you have to name each of these global hashes (and I am bad with names).
#!/usr/bin/perl use strict; use Benchmark qw(:all); our $hash = { foo => "results for foo", bar => "results for bar", qaz => "results for qaz", }; sub globalhash { my ( $x ) = @_; $hash->{$x} || "... and so on, ad nauseum"; } sub anonhash { my ( $x ) = @_; { foo => "results for foo", bar => "results for bar", qaz => "results for qaz", }->{$x} || "... and so on, ad nauseum"; } sub ternary { my ( $x ) = @_; return ( $x eq 'foo' ) ? "results for foo" : ( $x eq 'bar' ) ? "results for bar" : ( $x eq 'qaz' ) ? "results for qaz" : " ... and so on, ad nauseum"; } my @strings = qw/foo bar baz qaz/; my $i = 0; cmpthese( -5, { anonhash => sub { anonhash( $strings[$i++] ); $i = 0 if ( $i == @strings ) }, ternary => sub { ternary( $strings[$i++] ); $i = 0 if ( $i == @strings ) }, globalhash => sub { globalhash( $strings[$i++] ); $i = 0 if ( $i == @strings ) }, } );
In reply to Re^2: How to access a static hash.
by gam3
in thread How to access a static hash.
by gam3
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |