Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Question about curious performance of if...elsif block

by BrowserUk (Patriarch)
on Jan 16, 2012 at 19:17 UTC ( [id://948196]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Question about curious performance of if...elsif block
in thread Question about curious performance of if...elsif block

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?

Replies are listed 'Best First'.
Re^4: Question about curious performance of if...elsif block
by markseger (Beadle) on Jan 16, 2012 at 19:34 UTC
    ok, one more time. try embedding my subruotine in a loop reading /proc/pid/status and measure it. THAT's my point. In a small program the timings are as expected. My question is why the unexpected performance when reading /proc?

    Did you try my second script on your system? Try that without the return statement.

    -mark

      Did you try my second script on your system?

      No. Because it would not run here.

      ok, one more time. try embedding my subruotine in a loop reading /proc/pid/status and measure it. THAT's my point.
      Then it is fairly obvious that the reason for the differences you are concerned about have nothing to do with that subroutine and every thing to do with the code that is calling it. Ie. All your concentration on the subroutine is a complete red herring.

      It seems fairly clear to me that you are misinterpreting the data you are reading. What timing do you see if you comment out the subroutine call entirely?

      But don't bother with "ok, one more time.". Cos I almost certainly wouldn't understand it.


      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?

        > No. Because it would not run here. ahh, so I guess you're not running linux.

        no problems. here's a run with the subroutine call commented out:

        [root@poker collectl]# time ./test.pl real 0m14.464s user 0m5.089s sys 0m9.087s
        now with the call executing BUT the return statement executing first thing in the subroutine:
        [root@poker collectl]# time ./test.pl real 0m19.508s user 0m10.461s sys 0m8.853s
        so clearly the overhead in calling the subroutine 1M times is about 5-1/2 seconds. Now I'll comment out the early return in the subroutine in the subroutine and run it again:
        [root@poker collectl]# time ./test.pl real 0m25.648s user 0m16.350s sys 0m9.123s
        This says to me the if...elsif... takes about 6 seconds and I can't fathom why! Could there be some weird interaction going on between perl/linux and the /proc filesystem?

        As I said in my base note I'm not an innards kind of a guy but it feels like something that is not going to show up in every perl script. Rather if feels like there is some kind of weird interraction.

        and speaking of environment this is rhel5.3 and perl 5.8.8. I just tried the code again on rhel6.2 in a VM with perl 5.10, and the differences between the last 2 runs are:

        real 0m7.359s user 0m2.840s sys 0m4.481s real 0m9.782s user 0m5.082s sys 0m4.615s real 0m11.927s user 0m7.266s sys 0m4.604s
        so in this case both the subroutine calls and the if...elsif... block are only taking a little over 2 seconds each. This says to me there is something more efficient going on with 5.10.

        Perhaps this is less of a big deal as 2 extra seconds is getting less of a concern, but I still an curious about what exactly is going on.

        -mark

Log In?
Username:
Password:

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

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

    No recent polls found