Perl reference vs. direct access vs. return Benchmark

This is a little Perl Script where I try to test what way is faster,  I test a normal reference(alias) in a sub vs. the direct access vs. normal access to a variable in a sub and then return the result, here is my Benchmark code:

Our test result:

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.

Perl strict Benchmark

This is a Example-Benchmark about to turn off things you don’t need in Perl:

Our Result:

Maybe a good solution is to use it to check your code and then to comment it out, maybe with a debug variable, for this take a look at Perl Debug Output.

(This is just an Example, don’t turn of use strict.)

Perl sub Benchmark

This is a Benchmark about the subroutines in Perl:


We see the normal way is the best solution.

Perl bignum Benchmark

This is a Benchmark in Perl about bignum :

Our result:

We see the cost is about 12% performance, so if its not really necessary we should tun this feature off.

Perl Benchmark IN sub with grep

If you miss the IN function in Perl like the function in PL/SQL, then you could use grep and an array to check this:

I made a Benchmark to check how fast his solution is:


We see the normal way is the fastest and you could use an defined array to speed up by 7%. The Code is short but much slower than an or comparison.

Perl && vs. and – || vs. or Benchmark

I want to compare the && vs. the and operator, and the || vs. or operator in Perl:


First result (1 && 0) vs. (1 and 0):

Second result (1 or 0) vs. (1 || 0):

Third result (1 && 1) vs. (1 and 1):

Fourth result (1 or 1) vs. (1 || 1):

It seems the “and” and “&&” don’t make really different, but the “or” and “||” operator seems different.I don’t know why the “||” are 18-48% faster than the “or” …. and the “and” is only about 1-3% faster.


Now we check if they work as they should:

result looks good they abort if a true is impossible:

That is more a check up and not really a Benchmark.

Perl reference vs. handing over Benchmark

I want to know if there is a different between a shift and normal handing over to a sub in Perl.This is only for scalar, for big data(array,hash) there is of course a different.

Our result:

If its not necessary we should avoid references for scalar types, and try to iterate and not shift: Perl shift iterate Benchmark.But for Skalar values it is the fastest way to use prototypes.

perl map vs for Benchmark

I want to test what’s the different between a map function and a for iteration with Perl is:

Our Benchmark result:

map is about 11% faster than a simple for loop.

Perl multiplication vs. division Benchmark

Today I want to compare the multiplication and division operation in Perl both could do the same, as example “100*2” is the same as “100/0.5”. And “100*0,5” is the same as “100/2”. Now let’s see what’s the Benchmark says:

We see in bout cases the multiplication is much faster:

To change the divisor to a factor calculate : factor = 1/divisor.This could be also useful if you have the same divisor for some calculations.