DaWolf has asked for the wisdom of the Perl Monks concerning the following question:

Greetings.

I was reading a PHP article (before you guys downvote and flame me please keep reading) and I saw this code snip:
for($i=1; $i<($messages+1); $i++) { ... }
An interesting question arised: I'd do this code snip like this:
for($i=1; $i<=$messages; $i++) { ... }
But then I've remebered my algorythm classes, when my teacher stated that you should always avoid the use of <= or >= because using them makes your program do twice the job, and therefore eat more machine resources, etc, etc...

I'd like to hear some opinions about this.

Thanks in advance

my ($author_nickname, $author_email) = ("DaWolf","erabbott\@terra.com.br") if ($author_name eq "Er Galvão Abbott");

Replies are listed 'Best First'.
Re: Better use of for()
by bart (Canon) on Oct 31, 2003 at 15:12 UTC
    Rubbish. Twice. First, <= can be a single assembler comparison instruction (<= is the same as not >). Even if it wasn't, this is a ridiculous attempt at micro-optimization.

    And second: do you really want to calculate the expression 1+$messages every single time?

    My guts tell me it's impossible for $foo<(1+$messages) to be faster than $foo<=$messages. On the contrary. And then, execution time will be very low for both, compared to the time spent in the rest of the script. This is pretty much a waste of time.

Re: Better use of for()
by Limbic~Region (Chancellor) on Oct 31, 2003 at 15:16 UTC
    DaWolf,
    If the block only contained one statement, I would do:
    print "hello" for 1 .. $messages;
    If it was a multi-statement block, I would use:
    for ( 1 .. $messages ) { ... }
    I am not an internals person, so I can not attest to how many more operations testing less than is in comparison to less than or equal to, but I can tell you that premature optimization is often futile and any language where this made a noticeable difference probably isn't worth using.

    If you are a C style for loop fanatic and you really want to Benchmark the difference, compare it to this:

    #!/usr/bin/perl -w use strict; my $messages = 10_000; my $limit = $messages + 1; for ( my $i = 1; $i < $limit; $i++ ) { ... }
    Cheers - L~R
Re: Better use of for()
by hardburn (Abbot) on Oct 31, 2003 at 17:52 UTC

    . . . when my teacher stated that you should always avoid the use of <= or >= because using them makes your program do twice the job . . .

    Did this teacher of yours happen to know any ASM?

    Right out of the Intel Op Codes (external link):

    JL == less then JLE == less then or equal

    There are similar op codes for > and >=. You'll see similar op codes, often with the same name, in almost any architecture out there. The two pairs of op codes usually take the same ammount of cycles. I see no reason to avoid the use of <= or >=.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated