Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hashes with dispatch tables (slow sub calls) and if-elsif-chains (slow linear testing) shouldn't be as fast as this approach.

Dear Rolf, I know that you are interested in functional programming possibilities in Perl, so please stop taking for granted what HOP opponents are hammering constantly. Subroutine calls of course add some time penalty, but not as much as many people think or say. In fact, as soon as the routine is really doing something, the sub call penalty becomes almost anecdotal or even almost negligible compared to the rest or the processing. In the benchmark below, the dispatch table ranks second best, immediately after direct array search, although it does really nothing more than returning a value.
use strict; use warnings; use Benchmark qw/cmpthese/; my @translation = qw / Zero One Two Three/; my %trans = (1 => "One", 2 => "Two", 3 => "Three"); my @dispatch = ( sub {return "Zero"}, sub {return "One"}, sub {return +"Two"}, sub {return "Three"} ); sub test1 { my $var2 = shift; return ("One") if ($var2 =~ /^1/ ); return ("Two") if ($var2 =~ /^2/ ); return ("Three") if ($var2 =~ /^3/ ); return undef; } sub test2 { my $var2 = substr shift, 0, 1; return ("One") if ($var2 == 1 ); return ("Two") if ($var2 == 2 ); return ("Three") if ($var2 == 3 ); return undef; } sub test3 { return $translation[(substr shift, 0, 1)]; } sub test4 { my $var = shift; return $trans{$1} if $var =~ /^(\d)\./ ; } sub test5 { my $var = substr shift, 0, 1; eval { goto "_$var" } or return "Other"; _1: return "One" ; _2: return "Two" ; _3: return "Three"; } sub test6 { return $dispatch[(substr shift, 0, 1)]->(); } cmpthese( -1, { _linear_1 => q {test1("2.01.000")}, _linear_2 => q {test2("2.01.000")}, _array => q {test3("2.01.000")}, _hash_regex => q {test4("2.01.000")}, _goto => q {test5("2.01.000")}, _dispatch => q {test6("2.01.000")}, } )
And the results:
$ perl test_if.pl Rate _hash_regex _goto linear_1 linear_2 _dispatc +h _array _hash_regex 831494/s -- -29% -46% -52% -54 +% -70% _goto 1173439/s 41% -- -24% -32% -36 +% -57% _linear_1 1538868/s 85% 31% -- -10% -1 +6% -44% _linear_2 1714704/s 106% 46% 11% -- - +6% -37% _dispatch 1827212/s 120% 56% 19% 7% - +- -33% _array 2735932/s 229% 133% 78% 60% 50 +% --
The dispatch table approach, with its sub call penalty, is 33% slower than the direct array access, but still quicker than any of the other tested approaches.

In reply to Re^2: Given When Syntax by Laurent_R
in thread Given When Syntax by Deep_Plaid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (6)
    As of 2019-09-23 20:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The room is dark, and your next move is ...












      Results (285 votes). Check out past polls.

      Notices?