The Perl You Need to Know: Benchmarking Perl Page 2

Download the authoritative guide: Data Center Guide: Optimizing Your Data Center Strategy

Download the authoritative guide: Cloud Computing: Using the Cloud for Competitive Advantage

In the above example, the desired result will appear in the special variable , since it is the first (and only) match possible from this regular expression. Then your mind starts to wander ... what if you lopped off the righthand portion using a split rather than a regular expression? What if you used a substr rather than a split? What if you get pizza tonight and swear that you'll cook something from scratch tomorrow?

Enter the Benchmark. Using the Benchmark module's timethis function, we can measure the execution time of one of these solutions.

        use Benchmark;
Same code as before, but this time supplied to the timethis function. The first parameter supplied tells Benchmark how many times to run this code. The more times it runs the code, the more accurate a measurement is revealed. In fact, if we run the line above, Benchmark will warn sternly: too few iterations for a reliable count. Indeed, we should supply a higher number of run-throughs. Nice and high, like 500000 -- that's right, five hundred thousand!
Once done, Benchmark reports:
        timethis 500000: 1 wallclock secs( 1.19 usr + 0.01
         sys = 1.20 CPU) @ 415973.38/s (n=500000)
The results? Over 500,000 iterations, this snippet of code runs at a rate of 415,973.38 times per second. Of course, this number may vary if you try this test on your own machine, depending on its resources, the version of Perl being used, and so on. These particular examples were generated on a 900Mhz Athlon Thunderbird running ActivePerl 5.6.0 under Windows 2000. Rather then get hung up on the execution time taken on its own, what we're really interested in is how this code fares relative to the alternative solutions.

Another way to grab the filename extension is using split on the decimal, and grabbing the second item of the resulting array:

        =@{[split /\./,]}->[1];
Let's time it!
        use Benchmark;
          =@{[split /\./,]}->[1]');
The timesheet says:
        timethis 500000: 3 wallclock secs ( 2.59 usr + 0.00
         sys = 2.59 CPU) @ 192752.51/s (n=500000)
A fascinating result - between the tortoise and the hare, the split function was clearly the tortoise. Over 500,000 iterations, this solution only clocked in at 192,752.51 times per second: less than half the speed of the regular expression!

This article was originally published on Jan 23, 2001

Thanks for your registration, follow us on our social networks to keep up-to-date