Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Foreach Loop Optimization

by tirwhan (Abbot)
on Aug 01, 2007 at 15:27 UTC ( [id://630090]=note: print w/replies, xml ) Need Help??


in reply to Foreach Loop Optimization

You could replace lines 11/12 with

$localtime = $moduleField[2] ? FormatTime($moduleField[2]) : "n/a";

and also put your printout data into a variable and only print once per loop iteration instead of every time you want to add output. But I have a hunch that those changes will hardly matter and the real timewaster in this loop is the FormatTime subroutine (which you haven't shown us). I suggest using DevelDProf to find out what your code spends most of its time doing.


All dogma is stupid.

Replies are listed 'Best First'.
Re^2: Foreach Loop Optimization
by upallnight (Sexton) on Aug 01, 2007 at 15:57 UTC
    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.
      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

      Negligibly so. It's a readability issue.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://630090]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found