Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: How to access a static hash.

by gam3 (Curate)
on Mar 18, 2007 at 16:49 UTC ( [id://605369]=note: print w/replies, xml ) Need Help??


in reply to Re: How to access a static hash.
in thread How to access a static hash.

What you did not test is the global hash:
              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 ) }, } );
-- gam3
A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://605369]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-16 15:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found