Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Structure is more important than speed

by Anonymous Monk
on Sep 09, 2002 at 11:39 UTC ( [id://196212]=note: print w/replies, xml ) Need Help??


in reply to Structure is more important than speed
in thread Which way is faster?

The Perl interpreter is about as stupid as you can get.

Optimization is done at compile time, and compile time is seen as run time, so little optimization is done. Shorter variable names and hash keys are faster. If you throw away warnings you can double speed with

if (defined(my $foo = exists $lookup{$bar})) { .... }
instead of
if (exists $lookup{$bar}) { my $foo = $lookup{$bar}; ... }
In looping through nested data structures you gain speed by pulling out substructures once rather than doing nested lookups. Bonus: this may be clearer.
for my $foo (sort keys %some_hash) { my $by_foo = $some_hash{$foo}; for my $bar (sort keys %$by_foo) { my $by_bar = $by_foo->{$bar}; for my $baz (sort keys %$by_bar) { ... } } }
You can travel synchronized data structures in parallel rather than traversing one and doing hash lookups to find the other.

Good news! Using strict encourages fast lexicals.

Yes, I have needed to know this. A report took 3 hours per period, and needed to take less to finish the total job each night. There is no point in running more CPU-bound processes than one has CPUs. Moore did not arrive in time. So the code ran a half-dozen times faster with limited damage done. I did not rewrite in C.

Replies are listed 'Best First'.
Re: Re: Structure is more important than speed
by sauoq (Abbot) on Sep 09, 2002 at 21:34 UTC
    If you throw away warnings you can double speed with
    if (defined(my $foo = exists $lookup{$bar})) { .... }
    instead of
    if (exists $lookup{$bar}) { my $foo = $lookup{$bar}; ... }

    Maybe you can, but it hardly matters as they do two entirely different things. The first sets $foo to either 1 or "" (either of which is always defined.)

    -sauoq
    "My two cents aren't worth a dime.";
    
      D'oh. Thinko causing typoing. Make the obvious fix and the comment remains true.
        D'oh. Thinko causing typoing. Make the obvious fix and the comment remains true.

        I don't see an "obvious" fix that will make

        if (defined(my $foo = exists $hash{$key})) { # ... }
        work like
        if (exists $hash{$key}) { my $foo = $hash{$key}; # ... }

        I guess that's either because there isn't one or because I'm not enlightened. Would you enlighten me?

        Before you answer: Please notice that exists $hash{$key}; is not equivalent to defined $hash{$key};.

        -sauoq
        "My two cents aren't worth a dime.";
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found