I'm trying to understand why a block of if...elsif statements seem to be running slow, at least to me. So I conducted a few experiments and don't know enough about the innards of perl to explain why I'm seeing what I'm seeing or how I might be able to improve things.

First the basics - here's a very simple script that executes these statements 1M times inside a subroutine:

#!/usr/bin/perl -w for (my $i=0; $i<1000000; $i++) { test($i) } sub test { 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) {} }
and the difference in the times I see to execute it with and without the return statement commented out is about 3/4 of a second, so that's my approximate baselevel for executing the block of code.

The real question is I have a script that reads /proc/pid/status for all processes on my system, about 200 of them. There are 36 entries in the file and I execute the reading in a loop 1440 times (the equivalent of doing this once a minute for a day, in case anyone cares). This comes to executing that block about 1M times which is the reason for using that number in my test script above.

#!/usr/bin/perl -w my $data; for (my $i=0; $i<1440; $i++) { opendir PROC, '/proc'; while (my $pid=readdir PROC) { next if $pid!~/^\d/; open STAT, "</proc/$pid/status" or next; while ($data=<STAT>) { test($i); } close STAT; } } sub test { my $a=shift; 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) {} }
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.

-mark


In reply to 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.