On Tue, Jan 01, 2008 at 09:11:58PM -0800, Prabu Ayyappan wrote:

> Hi All,
> Below are some of the way to optimize the perl code. You can add more to
> this if you have something more.

Just a few comments ...

> 1) use a reference instead of the variable directly
> Use reference in passing large arrays in a function call. Because without a
> reference it will copy the entire array or hash onto the function call
> stack, and then copy it again in the function.

That depends on how you have written the subroutine.  You don't *have*
to copy the arguments in the sub.

> Using reference will also saves MEMORY.

And change the behaviour.  That's fine provided you're OK with that.

> 2) Static string Handling
> use single quotes rather than doubles. Double quotes force Perl to look for
> a potential interpolation of information, which adds to the overhead of
> printing out the string.
> Print ‘I am a static string I don’t want to get interpolated’;
> If we want to interpolate then do it as like this
> Print ‘I am a static string’ , ”\n” , ‘I don’t want to get interpolated’;

Who told you this?  I'd be surprised if you could measure any
performance difference between using single and double quotes when there
is no interpolation going on, and you certainly won't measure any
*runtime* performance difference.

Some people get religious about this as a matter of style.  Ignore them.

Passing a list to print() rather than concatenating the arguments is
probably faster, but is unlikely to make the difference between an
application that is too slow to use and one which is satisfactory.

> 3) Many if statements can be incredibly time consuming 
> if ($a > 0)
> {  $c = $a; }
> elsif ($b > 0)
> {  $c = $b; }
> else
> {   $c = $d; }
> This can be time consuming, waste of space. 
> Can be replaced with  $c = $a || $b || $d;
> If $a is a true value, Perl doesn't even look at the other variables.
> If $a is false, then Perl checks the value of $b and so on until it
> gets to the last value, which is always used, whether it's true or
> not.

Which isn't quite the same as what you had originally.

>  Best Regards,
> Prabu.M.A

I wonder why people get so hung up about speed.

Here's my tip:  Don't optimise until you know that what you have is too
slow/large/whatever.  Optimise the algorithm first.  Don't optimise for
speed until you have profiled.  If you really do have to optimise, it
pretty much comes down to reducing the number of opcodes.  At this
point it might be cheaper to throw hardware at the problem.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to