in reply to performance - loops, sub refs, symbol tables, and if/elses

The biggest problem with your performance testing is that they do not appear to me to be equivalent?

In the OUTER-IF case, your check some flag, and then process your entire array as whichever type that flag indicates.

In the INNER-IF case, you check that flag for every item to decide how to process it.

Personally, I can see no logic at all to pushing sub references into an array and invoking them in a seperate loop later.

You end up calling exactly the same number of subroutines, except that you have to dereferece an array element to get a scalar and then dereference that to invoke the sub. On top of that, you have to build the array(s) to store the references in in the first place. It may take less space on the screen, but your increasing both memory and cpu requirements for no gain at all.

The last one is equally bewildering. You save the conditional for deciding which function to call, but replace it with a condition to test whether the coderef has a value and can be called!

Again, your making the decision as to the type outside the loop, so whichever type $type is set to (however it is set?), every item is being treated as that same type.

Whatever benchmarking you did do, if it was based on a comparison of these 4 peices of code was a complete waste of time as they do not do the same things. Also, if your really after performance, then dump the 2/3/? functions to process each type and call one function with all the code inside it.

If you can make your $type decision once and then process the entire array based on that decision as implied by three of your 4 peices of sample code, then move the seperate processing sections (wash/peal/code) and the loop into a single sub (which could branch internally if needed) and pass a reference to the data array and your code could become.

$type = determine_type(); { process_apples(\@items), last if $type eq 'apples'; process_bananas(\@items), last if $type eq 'bananas'; ... } #final stuff common stuff exit(0);

If you really needed to wring the last ounce of performance, you might find the computed goto and avoiding the sub call might gain you a % or two, but it probably wouldn't improve your readability.

If however, you need to make the decision of $type for every item as implied by your INNER-IF sample then

$type = determine_type(); for $item (@items) { process_apples($item), next if $type eq 'apples'; process_bananas($item), next if $type eq 'bananas'; ... } #final stuff common stuff exit(0);

..

I seekest thy wisdom

Erm...cut it with the cutesy examples and show us the real (or at least a good simulation of the) DATA, and an example of the type of processing you are doing.

Then you might get some useful answers.


Examine what is said, not who speaks.