I would have expected the difference between running this script with the return statement commented out and not commented out to be on the order of a second or less yet it's actually over 5 seconds. This seems like a huge difference and I'd like to know if anyone can tell me why and more importantly if there's a way to improve things.

The reason it takes so long -- although in my tests of your sample code show the difference to be far less than 5 seconds -- is because for the vast majority of the cases, $i = 10 through 1,440 (or 1,000,000), you are performing all nine comparisons before returning.

The simple solution is to avoid doing those comparisons 99% (or 99.99999^) of the time:

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) {} }

On my system this consistently runs even faster than your test() sub with the unconditional return (test2 below):

C:\test>junk37 test1 took: 0.67200 seconds test2 took: 0.30307 seconds test3 took: 0.36421 seconds C:\test>junk37 test1 took: 0.66500 seconds test2 took: 0.30315 seconds test3 took: 0.29603 seconds C:\test>junk37 test1 took: 0.67800 seconds test2 took: 0.30240 seconds test3 took: 0.29488 seconds C:\test>junk37 test1 took: 0.66600 seconds test2 took: 0.30331 seconds test3 took: 0.29491 seconds C:\test>junk37 test1 took: 0.66900 seconds test2 took: 0.30349 seconds test3 took: 0.29475 seconds

Update: Also, if your if/else cascade is bigger than 9, then you might consider using a dispatch table:

my @dispatch = ( sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, sub {}, ); sub test4 { return if $_[0] > 9; $dispatch[ $_[0] ]->( $_[0] ); }

This runs pretty nearly exactly the same speed as test3 above when the number of blocks is low (9 in this case), but once you get to around 20 or so, it starts to win more substantially.

And if your cases are less amenable to using an array, then you can use a hash instead.

But do construct the dispatch table outside the subroutine. Do it (every time) inside teh sub and it will slow to a crawl.


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: 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":



  • 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, details, 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, summary, 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.