On Wed, Apr 9, 2008 at 7:14 PM, Gunnar Hjalmarsson <[EMAIL PROTECTED]> wrote: > Chas. Owens wrote: snip > > There are at least three downsides to using the substr function: > > 1. multiple function calls > > 2. lots of typing > > 3. no guarantee that the input string is in the right format > > > > That comment surprised me. While I can agree that a regex makes it easier > to validate input, the difference in typing is insignificant, and the regex > is often more expensive to run than multiple calls for substr(). Please see > the benchmark below. snip
To be fair you must ensure the input data is in the right format before using the calls to the substr function. Every call to the substr function will degrade performance more than adding another capture to the regex. Rate substr regex substr 1042618/s -- -13% regex 1203020/s 15% -- #!/usr/bin/perl use strict; use warnings; use Benchmark qw<cmpthese>; my $str = '060001_002'; cmpthese -1, { substr => sub { return unless $str =~ /^[0-9]{2}[0-9]{3}[0-9]_[0-9][0-9]{2}$/; return substr($str, 0, 2), substr($str, 5, 1), substr($str, 8, 2); }, regex => sub { return $str =~ /^([0-9]{2})[0-9]{3}([0-9])_[0-9]([0-9]{2})$/; }, }; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/