Gossamer Forum
Home : General : Perl Programming :

foreach/while

Quote Reply
foreach/while
I'm making a perl module mainly out of boredom but also I'm touching up my OO skills and in the module I use......
Code:
foreach (sort keys %ENV) {
$args{SHOW_BLANK} == 0 and next if !$ENV{$_};
$env .= qq|$_ : $ENV{$_}\n|;
}
.........and was just wondering if there is any great speed difference between that and....
Code:
while (($key, $value) = each %ENV) {
$args{SHOW_BLANK} == 0 and next if !$value;
$env .= qq|$key : $value\n|;
}

......or is it insignificant?

It's great fun writing modules.....I've not made many but I'm just finding the joys of doing so!

The module I'm writing currently allows for easy showing of the %ENV's in different formats using something like:

my $env = new ENV;

print $env->print_env(STYLE => 'normal', SHOW_BLANK => 0, FONT => 'verdana', FONT_SIZE => 12);

Useless I hear you cry.......well yes it is but it makes printing %ENV's much quicker and is just fun to play about with.

Last edited by:

PaulWilson: Sep 15, 2001, 7:41 PM
Quote Reply
Re: [PaulWilson] foreach/while In reply to
Just run them a few hundred/thousand times and benchmark it Smile

Adrian
Quote Reply
Re: [brewt] foreach/while In reply to
Well I'd really love to.....honestly........but I just don't have the time :)
Quote Reply
Re: [PaulWilson] foreach/while In reply to
Oh come now....

perldoc Benchmark

The functionality is built right in ! Smile

--mark
Quote Reply
Re: [Mark Badolato] foreach/while In reply to
........hmm may just give it a whirl now......didn't realise it was that simple :)

Last edited by:

PaulWilson: Sep 16, 2001, 1:30 PM
Quote Reply
Re: [Mark Badolato] foreach/while In reply to
Hm slightly unexpected results. I would have said while would be quicker.

10000 loops of foreach code took: 1 wallclock secs ( 1.32 usr + 0.00 sys = 1.32 CPU) @ 7575.76/s

10000 loops of while code took: 2 wallclock secs ( 1.15 usr + 0.00 sys = 1.15 CPU) @ 8695.65/s

Last edited by:

PaulWilson: Sep 16, 2001, 1:29 PM
Quote Reply
Re: [PaulWilson] foreach/while In reply to
Phew not bad....

Stats for counting to 1000, 10,000 times:

10 wallclock secs (11.81 usr + 0.00 sys = 11.81 CPU) @ 846.74/s (n=10000)

Last edited by:

PaulWilson: Sep 16, 2001, 1:35 PM
Quote Reply
Re: [PaulWilson] foreach/while In reply to
while WAs quicker... you have to look at the CPU number, not the wallclock seconds (which is fairly useless) Smile
Quote Reply
Re: [Mark Badolato] foreach/while In reply to
Ah ok.....hehe

Thats confusing.....you'd think the less CPU the faster it was.

Last edited by:

PaulWilson: Sep 16, 2001, 1:37 PM
Quote Reply
Re: [PaulWilson] foreach/while In reply to
Also, while will be much better the larger your hash is. The `foreach (sort keys %ENV) {` needs to expand all the keys of %ENV into a list in memory and then sort that list.

The while just iterates through the hash. But, the while isn't sorted, and foreach is, so you aren't doing a fair comparison. Try removing the sort and see what happens.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] foreach/while In reply to
Ah good point, I missed sort.

New results are:

10000 loops of foreach took: 1 wallclock secs ( 0.93 usr + 0.00 sys = 0.93 CPU) @ 10752.69/s
10000 loops of while took: 1 wallclock secs ( 1.04 usr + 0.00 sys = 1.04 CPU) @ 9615.38/s

There doesn't seem to be much of a difference.