write fast code in Perl

In this article I want to show how to write fast Perl-Code from the first to the last line, you could use it to speed up existing Programs but its better to make it right from beginning.


Turn off things you don’t need

In Perl it is important to use debug output, but only for developing, after your code is written down and tested you could disable it with a constant like Perl strict Benchmark or Perl use output only for debugging.For this take a look at Perl debug output and  Perl debug Benchmark, it is a simple way to switch debugging and other features off and on.The Interpreter knows that it could never be true and removes it completely, its faster than an If.Take a look at Perl print Benchmark to see how it works, and you could see that you should not modify the Stack.Another way is that the Interpreter could calculate things before they needed like in Perl constant Benchmark.Another thing is Bignum it is useful because it gives you a better precision but it costs a bit more time, fore more look at Perl Bignum Benchmark.


Don’t modify the Stack

For Perl its hard way to modify the stack with pop push shift unshift,its important for functions and arrays, try to access the items directly and not to shift them down like Perl shift iterate Benchmark.


Use pre-increment over post-increment

Lets have a look at the post increment operator, he stores the value in a variable then increments the original value and return the stored variable, the pre increment has just to increment and return the result, its much faster.For an Benchmark look at Perl increment Benchmark.



References are useful if you have big data, and you want to access them in a sub so you have only to move the reference and not the data, but be carefully if you have just scalars you could waste time like this: Perl reference vs. handing over.Another point is that arrays are smaller and faster than hashes so use them if you could: Perl array vs. hash handing over Benchmark.But in my last Test i get the best result with prototypes.


Replace and Regex

For Regex you should use the o flag or qr// to avoid a new check of the Regex compiler, but you cant interpolate the regex like Perl replace benchmark.For replace you should use the y// over the s//, because its faster.To see what the difference in replace look at Perl replace vs. sed, awk and bash.


Choose a multiplication over a division

In the most cases you could use a multiplication instead an division and save time like Perl multiplication vs. division Benchmark.


Use map

If you want to alter an array and could use map, then use it it speeds up your script: Perl map vs. for Benchmark.


Loop with fixed range

If you dont have to modify an pointer in the loop or have anoter increment you should use the for with a fixed range like Perl loop Benchmark.


remove double entries

To remove double entries in an array look at this: Perl remove double entries in an array Benchmark.


Make it by hand

The last stepp is to make it by hand like Inline C or a Preprocessor, or you could do it in Perl like in this example: Perl grep Benchmark, here I used an manual wy over an grep.

15 thoughts on “write fast code in Perl”

  1. You should always run your code with use strict; enabled because it can help prevent bugs from becoming vulnerabilities.

    For example the following code will print the name of the program if strict mode isn’t enabled. my $a = 0; say $$a Just imagine if the value of $a came from an attacker. This is part of the reason I always tell people to always have use strict; and use warnings; in their code until they know exactly why they are recommended.

    Also having use strict; enabled also forces you to use lexical variables, which are faster than the default of having them be global variables.

    y/// and tr/// are exactly equivalent, that is they result in the same internal representation. So recommending one over the other on the basis of speed doesn’t make any sense. Also you benchmark code is too short to show any difference, as the majority of the time is from the mere fact that it is calling a subroutine.

    If you don’t believe me that y/// and tr/// are exactly the same run this code:

    perl -MO=Deparse -e 'y/1/X/'

    Using the /o modifier on regular expressions is asking for trouble.
    The reason it was added is that if you combined in a string from another variable that the whole regex would get compiled once, rather than every time it was executed. This is no longer necessary as perl doesn’t recompile the regex unless the variable changes. The only reason it has not been removed is for backwards compatibility. The trouble comes when you use it more than once, and expect the regex to use the current value of the variable instead of what the variable had the first time the regex was called.

    As for your which should be even faster. ( It’s also more correct in that it looks at undef separately from the empty string )

    1. My point is to remove things like debugging :”debugging and other features off and on”, because if you don’t need them you don’t have to load them (like data dumper,NYTProf or command line prints).

      I know that y// was the older version of tr//, but I tried it again and again I don’t know why I get this result.

      Yes, maybe its better to choose qr// over /o, I never had any problems with /o and its exactly the point to compile the regex to save time at the check if the interpolated variable has changed.

  2. Ahaa, its fastidious discussion concerning this piece of writing at this place at this webpage, I have read all that, so at this
    time me also commenting at this place.

Leave a Reply

Your email address will not be published.

11 − 11 =