[33013] in Perl-Users-Digest
Perl-Users Digest, Issue: 4289 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 26 09:09:13 2014
Date: Fri, 26 Sep 2014 06:09:02 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 26 Sep 2014 Volume: 11 Number: 4289
Today's topics:
Re: perl and math need help with printf or other <rweikusat@mobileactivedefense.com>
Re: somewhat amusing OO pitfall <rweikusat@mobileactivedefense.com>
Re: somewhat amusing OO pitfall <rweikusat@mobileactivedefense.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 25 Sep 2014 19:42:44 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: perl and math need help with printf or other
Message-Id: <87vbob8rtn.fsf@doppelsaurus.mobileactivedefense.com>
John Black <jblack@nospam.com> writes:
> In article <874mvx5g1t.fsf@sable.mobileactivedefense.com>, rweikusat@mobileactivedefense.com
> says...
>> while/ until (<condition>) {
>> }
>>
>> or
>>
>> do {
>> } while/ until (<condition>)
>>
>> whichever is more appropriate. And these two should be avoided in favour
>> of iterating over a list aka 'a foreach loop' unless there are good
>> reasons for not using one, eg, the list becomes so large that this is a
>> tangible performance impact, because the latter enables writing 'natural
>> loops' (process all available items) without having to deal with 'the
>> mechanics of moving the boxes', ie, explict initializations of loop
>> variables, explicitly written conditions and explicit step expression
>> code.
>
> Rainer, I cannot follow what you are saying here. Are you saying that foreach loops are
> better performing than while loops for iterating over a list?
'A list' is a somewhat hazily defined term. Depending on where the
values-to-be-iterated-over actually come from, a foreach loop may well
be faster than a more C-style one because more of the processing is done
by native code which is part of perl, example
-----------------
use Benchmark;
my @a = 1 .. 10000;
timethese(-3,
{
direct => sub {
my $s;
for (@a) {
$s += $_;
}
},
indirect => sub {
my ($i, $s);
for ($i = 0; $i < @a; ++$i) {
$s += $a[i];
}
}});
------------------
But the situation is somewhat different for printing a large string
part-by-part. A loop based on substr will work with one substring at a
time so the amount of additional memory needed for performing the algorithm does
not depend on the size of the string. OTOH,
print for /.{1, 70}/g
will create a list of all substrings of the input string on the stack
prior to looping (the same is true for generating any kind of list, as
opposed to 'iterating over an existing one') so this will need more than
twice as much memory as the substr-based variant for processing the same
input. But unless it is known that the inputs may be seriously large,
this should IMHO be ignored.
------------------------------
Date: Thu, 25 Sep 2014 13:46:59 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: somewhat amusing OO pitfall
Message-Id: <8761gbrhoc.fsf@doppelsaurus.mobileactivedefense.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
> you should change the do_print like this
>
> sub do_print
> {
> print_it __PACKAGE__ eq ref $_[0] ? @_[1_..$#_] : @_
> }
I don't like this idea. Perhaps this wasn't clear from the example, but
the method is actually part of an OO-interface to a lower-level,
procedural facility and the lower-level routine shouldn't be aware of
anything about the call to the higher-level routine. The actual method
ended up as
sub write
{
my $self = shift;
unshift(@_, $self->[0]);
&write_to_mm_file;
}
------------------------------
Date: Thu, 25 Sep 2014 13:54:09 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: somewhat amusing OO pitfall
Message-Id: <871tqzrhce.fsf@doppelsaurus.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>> you should change the do_print like this
>>
>> sub do_print
>> {
>> print_it __PACKAGE__ eq ref $_[0] ? @_[1_..$#_] : @_
>> }
>
> I don't like this idea. Perhaps this wasn't clear from the example, but
> the method is actually part of an OO-interface to a lower-level,
> procedural facility and the lower-level routine shouldn't be aware of
> anything about the call to the higher-level routine.
This doesn't make any sense here because George was writing about the
higher-level routine, I just didn't realize that because of the lack of
context.
> The actual method ended up as
>
> sub write
> {
> my $self = shift;
>
> unshift(@_, $self->[0]);
> &write_to_mm_file;
> }
which - according to some tests I made - is faster than the method
suggested above (and less verbose).
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 4289
***************************************