in reply to Variable declaration optimization?

You left out the following option:

my( $var1, $var2 ) = ( subroutine0_with_return_value(), subroutine1_with_return_value() );

None of them is appreciably better than the other from a CPU or memory perspective. When optimizing code for speed, you should first determine where the bottlenecks are (profile), and then look at ways of reducing that portion of the code's Big-O footprint. Optimizing something from O(1) to O(1) is Big-O-Meaningless. But taking a O(n^2) algorithm and replacing it with something O(n) is meaningful. Besides, if you're relying on nuances between my( $var1, $var2 ); and my $var1; my $var2, you're betting on undocumented performance nuances remaining equal from one version of Perl to the next, when in reality there are no guarantees that such undocumented nuances won't change in the future.


Dave

Replies are listed 'Best First'.
Re^2: Variable declaration optimization?
by PyrexKidd (Monk) on Oct 13, 2010 at 02:04 UTC
    I think this one looks the cleanest:
    my( $var1, $var2 ) = ( subroutine0_with_return_value(), subroutine1_with_return_value() );

    but I'm lazy and I find that:

    my $var1 = sub_w_ret_val1(); my $var2 = sub_w_ret_val2();

    is the easiest to think through, especially when assigning several return values.

    but I was taught that:

    public static void main(){ int var1, var2; //declare more variables here var1 = sub_w_ret_val(); var2 = sub_w_ret_val(); }
    (note the language change)

    was the correct method for declaring variables.

    while I'm not saying I find the third is the best method for declaring and using variables, I sometimes find myself doing the Perl (is it Perl or perl?) equivalent.

    my $file1, $file2 my $var1, $var2 $var1 = &sub_w_ret_val(); $var2 = &sub_w_ret_val();

    any way, that's my two cents on code writing optimization.

      That third example is the vestigial remnant of ancient compilers and language versions which care more about what the computers of the day used to do than usability.

      (Your final example has prototype behavior you likely did not intend.)