in reply to Re: Foreach Loop Optimization
in thread Foreach Loop Optimization

Would that line be more efficient than my two lines of code?

Thanks for the code suggestions. It was the print command I guess because of the latency from the server to my browser.

Replies are listed 'Best First'.
Re^3: Foreach Loop Optimization
by johngg (Canon) on Aug 01, 2007 at 16:34 UTC
    Doing a quick benchmark seems to show that it is a little bit more efficient, probably because you are only having to test once. It is certainly more readable.

    use strict; use warnings; use Benchmark q{cmpthese}; my @states; push @states, int rand 2 for 1 .. 10000; cmpthese(-5, { tirwhan => sub { foreach my $mf2 ( @states ) { my $lt = $mf2 ? q{processed time} : q{n/a}; } }, upallnight => sub { foreach my $mf2 ( @states ) { my $lt; $lt = q{processed time} if $mf2; $lt = q{n/a} unless $mf2; } }, });

    Here's the output

    Rate upallnight tirwhan upallnight 22.6/s -- -22% tirwhan 29.1/s 29% --

    (Usually when I post benchmarks someone points out that I've got it completely wrong so I'll probably get shot down in flames for this ;-)

    Cheers,

    JohnGG

Re^3: Foreach Loop Optimization
by ikegami (Patriarch) on Aug 01, 2007 at 16:17 UTC

    Negligibly so. It's a readability issue.

    And more likely it's the time needed to render the table than the actual communication time.