Perl scripts benchmarking
Par Pierrick, lundi 3 juillet 2006 à 16:22 / categorie: Perl / tags: / #73 / rss
In Perl Best Practices, I've discovered the use of module Benchmark. This module offers a method to compare the time required to execute a list of subs. In this post gives an example of use of Benchmark and I also propose a script to benchmark scripts, not only subs.
Here is an example to compare unpack and split functions with Benchmark.
#!/usr/bin/perl use strict; use warnings; use Benchmark qw/cmpthese/; use constant RAW_LINE => '0000482005032030708847018-000000764930-0000079000'; use constant SEP_LINE => '000048;2030708847018;-;000000764930;-;0000079000'; sub unpack_line { unpack('a6 a6 a13 a1 a12 a1 a*', RAW_LINE); } sub split_line { split(';', SEP_LINE); } # run the benchmark during 5 CPU seconds cmpthese -5, { 'unpack' => 'unpack_line()', 'split' => 'split_line()', };
The output of this script is:
$ ./unpack_split.pl
Rate unpack split
unpack 340037/s -- -3%
split 351507/s 3% --
Which means that unpack performs 340,037 executions per second while split performs 351,507 executions. split is 3% faster than unpack.
This method to bench a piece of code is very efficient for small subs but is inadequate for long execution time subs. For example, I want to benchmark a piece of code that iterate on the 1,700,000 lines of a files. In this example, I ask to perform each method only 5 times (because a single iteration already takes between 10 and 20 seconds). With module Benchmark, so few iterations is said "too few iterations for a reliable count". So I've decided to write a benchmark launcher to compare full scripts execution times.
$ ./launcher.pl --conf=bench1.ini --nb_run=10
foo : 10.0
bar : 12.3
The average execution time for command labeled "foo" is 10.0 seconds (based on 10 executions). The configuraiton file is organized as follows:
[misc] nb_run: 100 time_format: %.1f [commands] foo: echo foo >/dev/null bar: echo bar >/dev/null
launcher.pl is available online with an example configuration file.
Commentaires
Aucun commentaire pour le moment.
Ajouter un commentaire
Les commentaires pour ce billet sont fermés.