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 ?