In software design, efficiency is everything. The more efficient your code is, the faster it will run and the quicker it will respond to user commands. Inefficient, slow code usually needs to be reviewed and tweaked to make it run faster. But all of this is only possible if there exists some "timing device", which can measure how fast or slow a particular block of code is going and provide accurate readings to guide the developer in his/her tweaking.
In this document, we're going to give you an easy way to time ("benchmark") your Perl code, by creating an interactive tool to measure how long it takes to execute a code block. This tool will use the Perl Benchmark.pm module to run a piece of code multiple times, timing the execution of each run and returning the average time taken. As you tweak the code, you can time it with this tool to see if it is going faster or slower.
Note: The Benchmark.pm module comes bundled with recent Perl versions. However, if you do not have it, you can download and install it by running the following commands at your Perl prompt:
perl> perl -MCPAN -e "install Benchmark"The Script
Begin by creating the following Perl script:
Listing A
#!/usr/bin/perl
# import Benchmark module
use Benchmark;
# ask for number of runs
print "Enter number of code runs:\n";
$runs = <STDIN>;
chomp ($runs);
# allow carriage returns within code
# by altering the EOL indicator
$/ = "#END";
# ask for code block
print "Enter code block (end with #END):\n";
$code = <STDIN>;
chomp($code);
# test code and display run time report
print "\n...Testing...\n";
timethis($runs, $code);
This script asks the user to input two items: the number of times to test the code, and the actual code block to be tested. Once these values have been entered, the Benchmark module's timethis() function is used to run the code for the requested number of iterations, and return a report of the time taken.
One point of interest in this script is the change made to Perl's input record separator to support multi-line code blocks. Normally, Perl uses a carriage return to signal the end of user input, but because code blocks will usually span multiple lines, it has been altered to instead recognize the special string #END as marking the end of the code block.
Here's an example of the tool above in action. The sample code block used below calculates the square root of the first 100 numbers. It is executed 5000 times.
Listing B
Enter number of code runs:
5000
Enter code block (end with #END):
for ($i=1; $i<=100;$i++) {
$res = sqrt($i);
}
#END
...Testing...
timethis 5000
: 5 wallclocksecs ( 4.56 usr + 0.00 sys = 4.56 CPU) @ 1096.49/s (n=5000)
The report generated by timethis() has two components: the number of CPU seconds, which tells you how long Perl takes to run the code N times, and the per-second data, which tells you how many runs take place per second. Obviously, the higher the second value, the faster your code is.
In the above example, it's clear that Perl took a hair under 5 seconds to run the code block 5000 times. Or, to put it another way Perl was able to successfully execute the code block 1096 times per second. If you are so inclined, you can use this data to perform more specific calculations—for example, the number of milliseconds for a single code run.
For developers who are always looking to squeeze maximum performance from their code, a tool like this can be very helpful. As the code is optimized, it can be tested to see if it is running faster or slower. The greater the number of runs in a single second, the more efficient the code is.



