Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

RE: Buzzcutbuddha(I Stand Corrected): C Style For and Foreach The Same

by buzzcutbuddha (Chaplain)
on Aug 19, 2000 at 00:34 UTC ( [id://28586]=note: print w/replies, xml ) Need Help??


in reply to RE: RE: Buzzcutbuddha(You provided disinformation): C Style For and Foreach The Same
in thread Finding the length of an array of arrays

I stand corrected. You are correct on your points. Sorry for the accusation. For those interested, here is the code and results from benchmarking.
code:
use Benchmark; sub foreach1000() { my @x = (1..1000); foreach(@x) { my $z = $_; } } sub for1000() { my @x = (1..1000); for(my $y = 0; $y < scalar(@x); $y++) { my $z = $x[$y]; } } sub foreach10000() { my @x = (1..10000); foreach(@x) { my $z = $_; } } sub for10000() { my @x = (1..10000); for(my $y = 0; $y < scalar(@x); $y++) { my $z = $x[$y]; } } timethese( 10000, { for1000 => 'for1000()', foreach1000 => 'foreach100 +0()', for10000 => 'for10000()', foreach10000 => 'foreach10000()' });
results:
Benchmark: timing 10000 iterations of for1000, for10000, foreach1000, +foreach10000... for1000: 59 wallclock secs (53.75 usr + 0.03 sys = 53.78 CPU) @ 18 +5.95/s (n=10000) for10000: 676 wallclock secs (537.40 usr + 0.23 sys = 537.63 CPU) @ + 18.60/s (n=10000) foreach1000: 33 wallclock secs (24.67 usr + 0.00 sys = 24.67 CPU) @ 4 +05.27/s (n=10000) foreach10000: 293 wallclock secs (250.74 usr + 0.21 sys = 250.95 CPU) + @ 39.85/s (n=10000)

Replies are listed 'Best First'.
(jcwren) RE: (128?) C Style For and Foreach The Same
by jcwren (Prior) on Aug 19, 2000 at 04:05 UTC
    I re-ran these benchmarks with a minor optimization on the 'scalar(@x)' portion of the loop. It basically proved that the compiler isn't performing an optimization on the limit term in the loop. I'm a little surprised that since the array isn't being modified, and no functions are being called, the compiler didn't optimize that out to a fixed value. Of course, I'm speaking as an old 'C' coder...
    Benchmark: timing 10000 iterations of for1000, for10000, for10000a, fo +r1000a, foreach1000, foreach10000... for1000: 73 wallclock secs (60.06 usr + 0.16 sys = 60.22 CPU) for10000: 795 wallclock secs (632.63 usr + 1.39 sys = 634.02 CPU) for10000a: 508 wallclock secs (398.29 usr + 0.89 sys = 399.18 CPU) for1000a: 45 wallclock secs (37.13 usr + 0.06 sys = 37.19 CPU) foreach1000: 30 wallclock secs (24.32 usr + 0.06 sys = 24.38 CPU) foreach10000: 360 wallclock secs (276.53 usr + 0.69 sys = 277.22 CPU) use Benchmark; sub foreach1000() { my @x = (1..1000); foreach(@x) { my $z = $_; } } sub for1000() { my @x = (1..1000); for(my $y = 0; $y < scalar(@x); $y++) { my $z = $x[$y]; } } sub for1000a() { my @x = (1..1000); my $len = scalar(@x); for(my $y = 0; $y < $len; $y++) { my $z = $x[$y]; } } sub foreach10000() { my @x = (1..10000); foreach(@x) { my $z = $_; } } sub for10000() { my @x = (1..10000); for(my $y = 0; $y < scalar(@x); $y++) { my $z = $x[$y]; } } sub for10000a() { my @x = (1..10000); my $len = scalar(@x); for(my $y = 0; $y < $len; $y++) { my $z = $x[$y]; } } timethese( 10000, { for1000 => 'for1000()', for1000a => 'for1000a()', foreach1000 => 'foreach1000()', for10000 => 'for10000()', for10000a => 'for10000a()', foreach10000 => 'foreach10000()' });


    --Chris

    e-mail jcwren

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://28586]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found