in reply to Using aliases in for loops or not?

who cares about efficiency on this scale!, readabiltiy must be more important than shaving off a few micro-seconds (if that),
also what do you do with nested for loops? your soon going to get confused if you dont alias $_.


Im so sick of my Signature...

Replies are listed 'Best First'.
Re^2: A question for the enlightened.
by jkva (Chaplain) on Feb 17, 2005 at 10:23 UTC
    Assembler programmers would spin in their graves generating electricity if they read this. Like I said in my previous post, it's the mindset that's important. Yes, they might me micro-secs, but micro-secs nonethless.

    I do not see where I am using a nested forloop though. It's just a foreach.

    -- Detonite

      Assembler programmers wouldn't program in Perl.

      The mindset is indeed important. And it's very important to program in a language that fits ones mindset. If your mindset is that microseconds are important, Perl is the wrong language for you.

      As for the actual differences:

      #!/usr/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; our @array = (1 .. 1000); foreach my $x (1 .. 10) { cmpthese -1, { alias => 'my $c = 0; foreach (@array) {$c += $_}', variable => 'my $d = 0; foreach my $var (@array) {$d += $var}' +, }; } __END__ Rate alias variable alias 3139/s -- -3% variable 3230/s 3% -- Rate alias variable alias 3139/s -- -2% variable 3199/s 2% -- Rate alias variable alias 3139/s -- -0% variable 3140/s 0% -- Rate alias variable alias 3140/s -- -0% variable 3143/s 0% -- Rate alias variable alias 3110/s -- -5% variable 3262/s 5% -- Rate alias variable alias 3083/s -- -5% variable 3229/s 5% -- Rate alias variable alias 3131/s -- -3% variable 3229/s 3% -- Rate alias variable alias 3083/s -- -5% variable 3229/s 5% -- Rate variable alias variable 2821/s -- -10% alias 3140/s 11% -- Rate alias variable alias 3140/s -- -2% variable 3200/s 2% --
      You'd need to do some very careful analysis before deciding which of the two will save you some micro-seconds. Your analysis isn't based on anything. Just because something isn't in the code doesn't mean it isn't done.

        Assembler programmers wouldn't program in Perl.

        Yes they would :)

      Your not, but given that foreach is just an alias to for, what do you do with nested foreach loops!



      Im so sick of my Signature...
        I see what you mean, I did not know I could use a for instead of a foreach. I will look into this. Thanks.

        -- Detonite