Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: How does variable definition affect Perl performance ?

by Joost (Canon)
on Dec 05, 2009 at 04:14 UTC ( #811197=note: print w/replies, xml ) Need Help??

in reply to How does variable definition affect Perl performance ?

Short answer: no and no.

First question: initializing with $var = undef. Absolutely first: why aren't you using my? Secondly: 0, "" and undef are distinct values that can interact differently with the rest of the code, Don't assume that you can use them interchangeably.

Second question: length($string) == 0 vs if ($string): this is a good example of the difference mentioned above: 0, "0", undef and "" are all logically false in perl, but "0" and 0 are of length 1 while undef and "" are of length 0.

As for performance; this kind of nitpicking is unlikely to give you a speedup even a couple of orders of magnitudes smaller than the time you need to spend to make the change and test that the code will still work correctly, even if your code is running continously for a year or two. Look at the overall algorithm and its performance characteristics and improve that instead. For instance, minimize disk I/O in favor of using more memory if you can: for any file that's read more than once you're probably better off reading it into an array or hash table and keeping it there.

Even if you can't do that right now because of memory limits, it may be more efficient in terms of money and time to add a couple of Gb of RAM to the machine instead of messing about with the code. RAM is cheap and it'll only get cheaper, and so is CPU power; a couple of days worth of development time will buy you a top-of-the-line new machine. An hour or two development time will buy you a couple of Gb of RAM.

  • Comment on Re: How does variable definition affect Perl performance ?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://811197]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-02-25 14:44 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (365 votes). Check out past polls.