Wednesday, August 29, 2007

Optimize for speed

Perl comes with a very handy module to help you evaluate the relative speed of different code snippets.

The Benchmark module comes with a function that will take a hash of code snippets and a number. The module will run each code snippet that many times and report the speed of each for you to compare.

use Benchmark;
$string = "The bitter end.\n";
$code{chomp} = 'chomp $string';
$code{regex} = '$string =~ s/\n$//';
timethese(10_000_000, \%code);

1 comment:

Shyam said...

Can u please explain abt basic perl Modules and packages detail with simple example.Ur information is very useful for me. Thank u