Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > number of starting tabs

Reply
Thread Tools

number of starting tabs

 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-17-2012
Ben Morrow <(E-Mail Removed)> writes:

[...]

> For the specific case of a single-character string, /^\t*/ followed
> by measuring the length of the matched section (in any of the ways
> already posted) is probably a better solution.


If you know the sensible solution, as this very strongly suggests, why
don't you post it?

------------
print($ARGV[0] =~ /^\t*/ && $+[0], " leading tabs\n");
------------

Instead of search for the first character which is not a tab or
searching for a non-empty sequence of leading tabs and having to
special-case 'no leading tabs' somehow in both cases, returning the length of a
possibly empty sequence of leading tabs always yields the correct
value directly.
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-17-2012
Ben Morrow <(E-Mail Removed)> writes:
> Quoth Rainer Weikusat <(E-Mail Removed)>:
>> Ben Morrow <(E-Mail Removed)> writes:
>>
>> > For the specific case of a single-character string, /^\t*/ followed
>> > by measuring the length of the matched section (in any of the ways
>> > already posted) is probably a better solution.

>>
>> If you know the sensible solution, as this very strongly suggests, why
>> don't you post it?
>>
>> ------------
>> print($ARGV[0] =~ /^\t*/ && $+[0], " leading tabs\n");
>> ------------

>
> At least two people have already posted solutions equivalent to
> that;


At least two similar solution where posted but this one is better, as
was explained in the text you deleted.

> I was looking for something more general. I would prefer
>
> my ($tabs) = $var =~ /^(\t*)/;
> say length $tabs;
>
> since I try to avoid @+, @- and $N where possible, but that's purely
> a matter of taste.


That's not 'purely a matter of taste'. The following two pieces of
Perl code are equivalent insofar the final value of $tabs is
concerned:

-----
$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.

The 'matter of taste' doesn't matter here because this is not a work
of art. It is a set of instructions supposed to cause a computer to
perform a calculation, or, more correctly, it matters only of
aesthetic preferences trump technical considerations.
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-18-2012
Rainer Weikusat <(E-Mail Removed)> writes:
> "George Mpouras" <(E-Mail Removed)> writes:
>> $tabs = 0;
>> $ARGV[0] =~ /[^\t]/ and $tabs = $-[0];
>>
>> print("$tabs starting tabs\n");
>>
>>
>> this is impressive.

>
> It shouldn't be.


Especially since it is broken : $tabs will be zero if the examined
string contains nothing but \t characters.
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-19-2012
Ben Morrow <(E-Mail Removed)> writes:
> 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


[...]

> As I have said many times before, if you are concerned about that level
> of efficiency Perl is almost certainly the wrong language to be using in
> the first place.


The statement "If you are concerned about the way perl executes
Perl-code, you shouldn't be using Perl" doesn't seem to make much
sense to me: I'm concerned about this precisely because I use Perl and
I'm (for hopefully obvious reasons) interested in being able to use it
for anything where technical concerns, execution speed of the code
being among them, don't require taking a much more time-consuming
'other route'. The perl VM is a tool I'm employing to solve technical
problems and the more I know about this tool the more effectively can
I use it.

> The first rule of optimisation is 'Don't'.


'Optimization' is a mathematical term and it means 'finding an optimal
solution to a certain problem'. It doesn't really have a clearly
defined meaning when being applied to programming. Chances are that I
agree with your opinion for the definition of 'optimization' you
happen to have in mind. But that would be a different question.

>> The 'matter of taste' doesn't matter here because this is not a work
>> of art. It is a set of instructions supposed to cause a computer to
>> perform a calculation, or, more correctly, it matters only of
>> aesthetic preferences trump technical considerations.

>
> All forms of writing, in natural or artificial languages, should be
> considered a work of art at some level. (Incidentally, this is the
> principle upon which the idea of copyright in computer programs is
> based.)


The principle upon which the idea of 'copyright' (or 'patentability
of') computer programs is based is "There's serious money to be made
here and competion in the marketplact is bad for maximizing ROI." Pro
forma, it rests on the assumption that code would be overwhelmingly
the result of 'individual creative expression'. Expressions like the
first one quoted above rightfully cast some doubt on this
concept. They're more akin to mathematical formulas which can't be
copyrighted (or patented, at least in theory), because they are
discovered and not invented.

> While material technical considerations are more important than
> questions of aesthetics, in this case, unless the code in question is
> part of an inner loop you have previously determined is causing a
> significant performance problem, there is no *material* technical
> difference between the two.


Unless the Titanic sank, there's no reason to assume it ever would.
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      08-21-2012
On Friday, August 17, 2012 1:33:07 PM UTC-7, Rainer Weikusat wrote:
> Ben Morrow <(E-Mail Removed)> writes:
>
> > Quoth Rainer Weikusat <(E-Mail Removed)>:

>
> >> Ben Morrow <(E-Mail Removed)> writes:

>
> >>

>
> >> > For the specific case of a single-character string, /^\t*/ followed

>
> >> > by measuring the length of the matched section (in any of the ways

>
> >> > already posted) is probably a better solution.

>
> >>

>
> >> If you know the sensible solution, as this very strongly suggests, why

>
> >> don't you post it?

>
> >>

>
> >> ------------

>
> >> print($ARGV[0] =~ /^\t*/ && $+[0], " leading tabs\n");

>
> >> ------------

>
> >

>
> > At least two people have already posted solutions equivalent to

>
> > that;

>
>
>
> At least two similar solution where posted but this one is better, as
>
> was explained in the text you deleted.
>
>
>
> > I was looking for something more general. I would prefer

>
> >

>
> > my ($tabs) = $var =~ /^(\t*)/;

>
> > say length $tabs;

>
> >

>
> > since I try to avoid @+, @- and $N where possible, but that's purely

>
> > a matter of taste.

>
>
>
> That's not 'purely a matter of taste'. The following two pieces of
>
> Perl code are equivalent insofar the final value of $tabs is
>
> concerned:
>
>
>
> -----
>
> $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;

--
Charles DeRykus
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-22-2012
"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];
}});
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tabs -vs- Spaces: Tabs should have won. rantingrick Python 95 07-18-2011 11:07 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM
List text files showing LFs and expanded tabs (was: Colorize expanded tabs) qwweeeit Python 2 12-14-2005 10:07 AM



Advertisments