Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Even with 36 cases, I still see nothing like the 5 second difference you apparently are:

#! perl -slw use strict; use Time::HiRes qw[ time ]; sub test1 { my $a = $_[0]; # return; if ($a==1) {} elsif ($a==2) {} elsif ($a==3) {} elsif ($a==4) {} elsif ($a==5) {} elsif ($a==6) {} elsif ($a==7) {} elsif ($a==8) {} elsif ($a==9) {} elsif ($a==10) {} elsif ($a==11) {} elsif ($a==12) {} elsif ($a==13) {} elsif ($a==14) {} elsif ($a==15) {} elsif ($a==16) {} elsif ($a==17) {} elsif ($a==18) {} elsif ($a==19) {} elsif ($a==20) {} elsif ($a==21) {} elsif ($a==22) {} elsif ($a==23) {} elsif ($a==24) {} elsif ($a==25) {} elsif ($a==26) {} elsif ($a==27) {} elsif ($a==28) {} elsif ($a==29) {} elsif ($a==30) {} elsif ($a==31) {} elsif ($a==32) {} elsif ($a==33) {} elsif ($a==34) {} elsif ($a==35) {} elsif ($a==36) {} } sub test2 { my $a = $_[0]; return ; if ($a==1) {} elsif ($a==2) {} elsif ($a==3) {} elsif ($a==4) {} elsif ($a==5) {} elsif ($a==6) {} elsif ($a==7) {} elsif ($a==8) {} elsif ($a==9) {} elsif ($a==10) {} elsif ($a==11) {} elsif ($a==12) {} elsif ($a==13) {} elsif ($a==14) {} elsif ($a==15) {} elsif ($a==16) {} elsif ($a==17) {} elsif ($a==18) {} elsif ($a==19) {} elsif ($a==20) {} elsif ($a==21) {} elsif ($a==22) {} elsif ($a==23) {} elsif ($a==24) {} elsif ($a==25) {} elsif ($a==26) {} elsif ($a==27) {} elsif ($a==28) {} elsif ($a==29) {} elsif ($a==30) {} elsif ($a==31) {} elsif ($a==32) {} elsif ($a==33) {} elsif ($a==34) {} elsif ($a==35) {} elsif ($a==36) {} } sub test3 { return if $_[0] > 9; my $a = $_[0]; if ($a==1) {} elsif ($a==2) {} elsif ($a==3) {} elsif ($a==4) {} elsif ($a==5) {} elsif ($a==6) {} elsif ($a==7) {} elsif ($a==8) {} elsif ($a==9) {} elsif ($a==10) {} elsif ($a==11) {} elsif ($a==12) {} elsif ($a==13) {} elsif ($a==14) {} elsif ($a==15) {} elsif ($a==16) {} elsif ($a==17) {} elsif ($a==18) {} elsif ($a==19) {} elsif ($a==20) {} elsif ($a==21) {} elsif ($a==22) {} elsif ($a==23) {} elsif ($a==24) {} elsif ($a==25) {} elsif ($a==26) {} elsif ($a==27) {} elsif ($a==28) {} elsif ($a==29) {} elsif ($a==30) {} elsif ($a==31) {} elsif ($a==32) {} elsif ($a==33) {} elsif ($a==34) {} elsif ($a==35) {} elsif ($a==36) {} } my @dispatch = ( sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, ); sub test4 { return if $_[0] > 9; $dispatch[ $_[0] ]->( $_[0] ); } my $start = time; test1( $_ ) for 1 .. 1e6; printf "test1 took: %.5f seconds\n", time - $start; $start = time; test2( $_ ) for 1 .. 1e6; printf "test2 took: %.5f seconds\n", time - $start; $start = time; test3( $_ ) for 1 .. 1e6; printf "test3 took: %.5f seconds\n", time - $start; $start = time; test4( $_ ) for 1 .. 1e6; printf "test4 took: %.5f seconds\n", time - $start; __END__ C:\test>junk37 test1 took: 1.70500 seconds test2 took: 0.30789 seconds test3 took: 0.29240 seconds test4 took: 0.29207 seconds

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re^3: Question about curious performance of if...elsif block by BrowserUk
in thread Question about curious performance of if...elsif block by markseger

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found