in reply to A Luser's Benchmarking Tale

I disagree it's worse that several ifs. Your benchmark shows that one 'last' takes more time than one integer compare. If you make $val equal to 1, and turn the =='s into =~'s, sub1 is faster than sub2 (at least, on my system). The price of a 'last' doesn't depend on the amount of if's nor on what you do in the expression belonging to the if, but it does matter with the multiple ifs.

The fact that if the if/elsif chain is faster won't surprise anyone, as no conditions after a match will be tested, nor will a label need to be searched for (as is done with last).

Abigail

Replies are listed 'Best First'.
Re: Re: A Luser's Benchmarking Tale
by Melly (Chaplain) on Nov 25, 2003 at 15:16 UTC

    Hmm, well I take your point (and I had tested with $val=1 - see original post), but nevertheless it strikes me as damning with faint praise ;)

    What you're basically saying is that labels can be faster than multiple ifs provided you have enough conditions and you make them slow enough (e.g. regex)

    Given that multiple ifs are a pretty stoopid thing to do (I only used them in my benchmark for comparison purposes), this is hardly a glowing recommendation for labels.

    BTW I rewrote my test script to use $ARGV[0] for $val, and ran it with val=1 and val=4 for both versions (== and =~). Here are the results (which confirm your observation) 9kb gif of graph

    Tom Melly, tom@tomandlu.co.uk