Friday, July 13, 2012

Is this speed boat or a row boat?

When I first started programming in PERL, you could say I was the subject of all types of qualified and unqualified code revues. This lead to me finding out that in PERL there is more than one way to do it, TIMTOWTDI if you prefer the acronym. So, how do you explain to a 20 year old veteran that just because he's been doing the code that way for years and it works, that its not necessarily true for this application? For that fact how do I know my own code is the correct direction to take?

There is no need to "Go Fish", PERL has a solution using one of its core modules Benchmark. This is a simple tool to evaluate PERL code snippets and see how fast they are running. A good place to start with is to single quote or to double quote, which is an argument that I have heard for many years, says to always use string literals over interpolated strings because interpolation requires extra processing. So to get this boat race started, I have put together a small script to see if we can use the Benchmark module to prove this or not.


# test.pl
#!/usr/bin/perl
use Benchmark qw(:all) ;

cmpthese(10000000, {
'literal' => sub{my $a = 'abcde'},
'interpolated' => sub{my $a = "abcde"},
});

We will use the cmpthese function to compare the two code snippets and see which one runs faster. As you can see, the cmpthese method uses two parameters, a large integer which is number of loops to run the code and a reference to a hash of code to be evaluated. Each key in the hash is a label for a referenced piece of code that will be compared.

[coder@perlbox ~]$ ./test.pl
Rate literal interpolated
literal 3984064/s -- -2%
interpolated 4065041/s 2% --

As you can see the results are not as expected, interpolated won, this is because not all systems were created or are running equally. As I referenced before, that older code works better is not necessarily true for all applications, means that everything the server is doing can impact performance measurements. But after running this test several times you will find out that literals do perform a little better in most cases.

The Benchmark module can also be used to establish the high water mark to gauge whether performance modifications to your code make sense, with this being said I hope it make my job of fishing for faster code easier.