Ask a Question related to PERL Miscellaneous, Design and Development.
-
David Oswald #1
Using 'my' within nested loops
I am curious about the amount of overhead created with the first example
below as compared to the second example below:
Example 1:
my $this;
my $that;
foreach $this (@somearray) {
foreach $that (@some_other_array) {
........
}
}
Example 2:
foreach my $this (@somearray) {
foreach my $that (some_other_array) {
.......
}
}
It seems to me that while the second example does a better job of limiting
the scope of variables just to the block in which they're applicable, the
second example does cause the construction of a variable each time the outer
foreach iterates.
Is the overhead caused by constructing and deconstructing a variable over
and over again as it comes into and falls out of scope a degree of extra
overhead that could hamper execution speed on tight loops? Is this even an
issue?
I apologize for using "constructing" and "deconstructing"... just a loose
use of those words.
Dave
--
DJO
David Oswald Guest
-
Nested loops?
Hi, I've been adapting a script which parses HTML and creates an RSS feed (http://www.perl.com/lpt/a/2001/11/15/creatingrss.html). Now I want... -
HTML::Template arbitraryily nested recursive loops
Can anyone suggest a way to display an arbitrarily deep nested loop structure using HTML::Template? Below is what I've tried. I have a data... -
Possible Bug w/ nested loops and queries
May have found a possible bug... can someone please verify? In a database I have two tables, temp1 and temp2 These tables can contain anything,... -
[PHP] nested for loops
your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to for ($j=0; $j < 5; $j++) Anyone ever do a nested for loop? $i =0; $j... -
nested for loops
Anyone ever do a nested for loop? $i =0; $j =0; for ($x=0; $x < 50; $x++){ echo ("1 to 50"); for ($j=0: $j < 5; $j++) { echo ("less than... -
Bob Walton #2
Re: Using 'my' within nested loops
David Oswald wrote:
> I am curious about the amount of overhead created with the first example
> below as compared to the second example below:
>
>
> Example 1:
> my $this;
> my $that;
> foreach $this (@somearray) {
> foreach $that (@some_other_array) {
> ........
> }
> }
>
>
> Example 2:
> foreach my $this (@somearray) {
> foreach my $that (some_other_array) {
> .......
> }
> }
>
>
> It seems to me that while the second example does a better job of limiting
> the scope of variables just to the block in which they're applicable, the
> second example does cause the construction of a variable each time the outer
> foreach iterates.
>
> Is the overhead caused by constructing and deconstructing a variable over
> and over again as it comes into and falls out of scope a degree of extra
> overhead that could hamper execution speed on tight loops? Is this even an
> issue?
Why don't you:
use Benchmark;
and find out?
........> Dave
....> --
> DJO
--
Bob Walton
Bob Walton Guest
-
Eric J. Roode #3
Re: Using 'my' within nested loops
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
"David Oswald" <spamblock@junkmail.com> wrote in
news:vhp11c9c2fgd7b@corp.supernews.com:
My off-the-cuff answer: Probably not. You might shave a millisecond or two> Is the overhead caused by constructing and deconstructing a variable
> over and over again as it comes into and falls out of scope a degree
> of extra overhead that could hamper execution speed on tight loops?
> Is this even an issue?
off each loop iteration, but unless you're up against a wall, that probably
won't make much difference. If it does, perhaps Perl is not the right
language for your application.
Disclaimer: I haven't done any timings; this is just a gut answer from
experience.
- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBPxyNoWPeouIeTNHoEQLcMACg9XzrzRKzESATDIkxobfkcg ZjOSsAoPJG
ezdXWoBHkusM1/npZQFSvhPR
=AB2E
-----END PGP SIGNATURE-----
Eric J. Roode Guest
-
David Oswald #4
Re: Using 'my' within nested loops
"Bob Walton" <bwalton@rochester.rr.com> wrote in message
news:3F1C88B9.5060706@rochester.rr.com...> David Oswald wrote:
>> > I am curious about the amount of overhead created with the first example
> > below as compared to the second example below:
> >
> > Example 1:
> > my $this;
> > my $that;
> > foreach $this (@somearray) {
> > foreach $that (@some_other_array) {
> > ........
> > }
> > }
> >
> >
> > Example 2:
> > foreach my $this (@somearray) {
> > foreach my $that (@some_other_array) {
> > .......
> > }
> > }
> >Thanks for the suggestion. I just tried it (and should have thought of that> Why don't you:
>
> use Benchmark;
>
> and find out?
>
myself).
Here's the test code I used:
#!/usr/bin/perl -w
use strict;
use warnings;
use Benchmark;
my ($count, $this, $that, $t0, $t1, $td, @array1, @array2);
foreach (1..4000) {
push @array1,$_;
push @array2,$_;
}
$count = 0;
print "Beginning test 1.\n";
$t0 = new Benchmark;
foreach $this (@array1) {
foreach $that (@array2) {
$count++;
}
}
$t1 = new Benchmark;
$td = timediff ($t1, $t0);
print "Test 1 took: ", timestr ( $td ), "\n";
$count = 0;
print "Beginning test 2. \n";
$t0 = new Benchmark;
foreach my $those (@array1) {
foreach my $these (@array2) {
$count++;
}
}
$t1 = new Benchmark;
$td = timediff ($t1, $t0);
print "Test 2 took: ", timestr ( $td ), "\n";
The number of iterations was sufficient to tie up about 30 seconds per test
block on my measly old Windows98 Pentium II/266 notebook (my Perl testbed).
I ran it a couple times. The first time I ran it, the first testcase
(without the "my"'s inside the loops) took 33 seconds and the second
testcase (with the my's in the loop) took 32. The second time I ran it the
results were reversed, 32 and 33. So I guess the answer is that putting my
inside a tight loop doesn't increase overhead and runtime. Of course I
don't know if that's always the case. It could be that my simplified test
interprets and compiles differently than if the loops had more complexity
inside. But that's probably a different discussion.
Thanks all.
Dave
David Oswald Guest
-
Brian McCauley #5
Re: Using 'my' within nested loops
"David Oswald" <spamblock@junkmail.com> writes:
IMNSHO you should _always_ put an explicit my() or our() on the> I am curious about the amount of overhead created with the first example
> below as compared to the second example below:
>
>
> Example 1:
> my $this;
> my $that;
> foreach $this (@somearray) {
> foreach $that (@some_other_array) {
> ........
> }
> }
>
>
> Example 2: foreach my $this (@somearray) { foreach my $that
> (some_other_array) { ....... } }
foreach iterator variable. If you use an existing lexically scoped
variable then Perl will, in effect, create _another_ lexically scoped
variable of the same name.
Your Example 1 is, in fact, interpreted more like:
my $this;
my $that;
foreach my $this (@somearray) {
foreach my $that (@some_other_array) {
........
}
}
IMNSHO Perl should emit a warning when it does this but it doesn't.
Yes, that's what most people would intuatively think. That's why I think we need> It seems to me that while the second example does a better job of limiting
> the scope of variables just to the block in which they're applicable, the
> second example does cause the construction of a variable each time the outer
> foreach iterates.
a warning.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
Brian McCauley Guest
-
David Oswald #6
Re: Using 'my' within nested loops
"Brian McCauley" <nobull@mail.com> wrote in message
news:u9adb6x0y5.fsf@wcl-l.bham.ac.uk...limiting> "David Oswald" <spamblock@junkmail.com> writes:
>
>
> Your Example 1 is, in fact, interpreted more like:
>
> my $this;
> my $that;
> foreach my $this (@somearray) {
> foreach my $that (@some_other_array) {
> ........
> }
> }
>
> IMNSHO Perl should emit a warning when it does this but it doesn't.
>> > It seems to me that while the second example does a better job ofthe> > the scope of variables just to the block in which they're applicable,outer> > second example does cause the construction of a variable each time thewe need>> > foreach iterates.
> Yes, that's what most people would intuatively think. That's why I thinkThanks, that explains a lot. So would it be accurate to say that the> a warning.
>
majority of the syntaxes that default to $_, are actually (behind the
scenes) calling something to the effect of my $_, and limiting its scope, or
is this nuance only applicable to foreach loops?
David Oswald Guest
-
Tad McClellan #7
Re: Using 'my' within nested loops
David Oswald <spamblock@junkmail.com> wrote:
> "Brian McCauley" <nobull@mail.com> wrote in message
> news:u9adb6x0y5.fsf@wcl-l.bham.ac.uk...>> "David Oswald" <spamblock@junkmail.com> writes:^^^^^> So would it be accurate to say that the
> majority of the syntaxes that default to $_, are actually (behind the
> scenes) calling something to the effect of my $_,
No.
my() and local/our() each refer to one of Perl's two systems of
variables. my() for lexical variables and local/our() for
package (dynamic) variables.
Most of Perl's built-in variables are package variables,
you _cannot_ my() them.
So, it is a local($_) that foreach gives you.
> and limiting its scope, or
Package variables are global variables.
What you are limiting is not the visibility of the variable, but
the visibility of the _value_ of the variable.
You are still open to classic action-at-a-distance bugs, which
is what happened to you. :-) (or should that be :-(
> is this nuance only applicable to foreach loops?
Which nuance?
Q: The automatic-localling of $_ by foreach?
A: map and grep do it too.
Q: the difference between my() and our() variables and values?
A1: as in the Perl FAQ
What's the difference between dynamic and lexical (static)
scoping? Between local() and my()?
A2: and "Coping with Scoping"
[url]http://perl.plover.com/FAQs/Namespaces.html[/url]
A3: Executive summary:
Always prefer my() variables over our() variables,
except when you can't.
(you can get good-old, sane scoping by using a lexical variable
for foreach's loop control variable: foreach my $str (...)
)
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
David Oswald #8
Re: Using 'my' within nested loops
"Tad McClellan" <tadmc@augustmail.com> wrote in message
<response snipped>
Thanks again Tad. I appreciate the clarification.
Dave
David Oswald Guest



Reply With Quote

