"C.DeRykus" <(E-Mail Removed)> writes:

> On Friday, August 17, 2012 1:33:07 PM UTC-7, Rainer Weikusat wrote:

>> > Quoth Rainer Weikusat <(E-Mail Removed)>:
[...]

>> -----

>>

>> $tabs = $ARGV[0] =~ /^\t*/ && $+[0];

>>

>> -----

>>

>> -----

>>

>> ($tabs) = $ARGV[0] =~ /^(\t*)/;

>>

>> $tabs = length($tabs);

>>

>> -----

>>

>> But they utilize different methods of calculating this value and while

>> the first requires perl (5.10.1) to perform nine basic operations, the

>> second needs fourteen, not the least because it reimplements a feature

>> the perl regex engine already provides in a relatively clumsy way in

>> Perl: There's no point in copying the substring or even just capturing

>> it if only the number of characters are supposed to be counted.

>

> I think the copy could be avoided though:

>

> $tabs++ while $var =~ /\G\t/g;
Leading remark: One the machine where I tested this, the absolute

difference are in he 1E-7 range which implies that this is a

scientific problem of some interest (to certain people, at least

but

each of the three variants is as suitable for any practical problem

where less than a couple of hundredthousands of inputs need to be

processed as the two others.

Regarding the last one: One should expect this to be distinctively

worse than the other two because even more 'algorithmic work' is

performed in Perl-code. And that was actually the result I got:

Averaged over for runs, the first ran at about 1.05 times the speed of

the second and at about 1.46 times the speed of the third

[second-to-third 1.39).

Test program

---------------

use Benchmark;

my $in = "\t\t\t\tbla";

my $t;

timethese(-5,

{

copy => sub {

($t) = $in =~ /^(\t*)/;

return length($t);

},

count => sub {

pos($in) = 0;

++$t while $in =~ /\G\t/g;

return $t;

},

calc => sub {

return $in =~ /^\t*/ && $+[0];

}});