Lately, a lot of my code has had to do a lot of conditional juggling, based on the values passed to it. This is a piece of example code:
[snip] And it's fast enough for me, but it has me wondering: is it faster to use elsif for this, or to just chain the statement into a whole pile of if statements?

It is faster to write in C. Well, not really: it will probably take longer to write your program in C, but it will execute faster. So, sorry for repeating what others already wrote in this thread, but they say that repetita iuvant: if you have good reasons to be concerned by this, then by all means you should switch to C, or some other language. Otherwise you shouldn't be concerned at all and repeat the premature optimization mantra quite a number of times.

However, to remain slightly more in topic: in Perl one seldom needs long chains of if's and elsif's; even "simple" else's are often not needed as there are alternative syntactical choices with the same semantics that make for much more readable, and maintainable, code.

BTW: two relevant quotes not as widely known as Hoare's one:

Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you have proven that's where the bottleneck is.
- Rob Pike

The First Rule of Program Optimization: Don't do it.
The Second Rule of Program Optimization (for experts only!): Don't do it yet.

- Michael A. Jackson


In reply to Re: Regarding speed: Is elsif or just if faster? by blazar
in thread Regarding speed: Is elsif or just if faster? by Spidy

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.