Prabu Ayyappan wrote:
Hi All,

Hello,

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

perldoc -q "How can I make my Perl program run faster"

perldoc -q "How can I make my Perl program take less memory"

perldoc -q "How can I free an array or hash so my program shrinks"

perldoc -q "Why does using $&, $‘, or $’ slow my program down"

perldoc -q "What’s wrong with using grep in a void context"

perldoc -q "What’s wrong with using backticks in a void context"

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.
Using reference will also saves MEMORY.
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’;
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.

Use the Benchmark module to test your assumptions. You may be surprised that some things that seem to be faster are actually not.


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


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


Reply via email to