|Don't ask to ask, just ask|
Re: Optimizing existing Perl code (in practise)by gmpassos (Priest)
|on Aug 19, 2002 at 11:47 UTC||Need Help??|
Well, if you really want to make some code faster make a XS, in other words make it in C. But this is only good to do with filters, crypters, etc...
To win speed, you can make tests of your code, specially inside loops, peaces that will be runned a lot of times, to find the best way to write it! Here are some tips:
For subs use the content of the @_, specially for big data sent to the function. If you want speed use first the @_, then if you need to change the data inside @_, you use my ($var) = @_ ;, and if you have big data you use the "shift".
If you have a loop (while,for,foreach) that will be runned a lot of times, try to not use the my inside it:
Don't use local(), my() is faster! The command local() in the begin of perl was used like my, but now it's only good if you want to make local *HANDLES, not variables.
Try to use the variables in this order: $scalar, @array, %hash. Some thimes we use %h or @a and they aren't needed, but they are more slower than $s and use more memory, specially %h!
About regular expressions (RE), use it only when it's needed! Dont make this: if($var =~ /x/) if you can do if($var eq 'x'). But some times RE can be faster than bigger codes, the best way to chose is test the 2 codes.
But always think that any tip here will improve some microseconds for you. Only spend time improving speed in the peaces of your code that really need! Always try to use the resources of core, don't remake things that can be made by Perl it self.
"The creativity is the expression of the liberty".