Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

RE: Buzzcutbuddha(You provided disinformation): C Style For and Foreach The Same

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


in reply to RE (tilly) 2: C Style For and Foreach The Same
in thread Finding the length of an array of arrays

I was correcting you because you said that foreach is faster, etc. I think it would have been better to say that foreach is Perl Style and easier to understand, and better to use for those reasons...

just my $0.02
  • Comment on RE: Buzzcutbuddha(You provided disinformation): C Style For and Foreach The Same

Replies are listed 'Best First'.
RE: RE: Buzzcutbuddha(You provided disinformation): C Style For and Foreach The Same
by tilly (Archbishop) on Aug 18, 2000 at 17:51 UTC
    Read what I wrote exactly as I wrote it the first time.

    I said that foreach loops are faster than classic C-style loops. Yes, you can write a classic C-style loop using foreach. But nobody thinks that way.

    And indeed iterating directly over a list is *significantly* faster, less error prone, etc, than doing an explicit loop over the indices, looking up array values inside the loop. That is not disinformation, that is simple fact and I have found that that factual information carries a lot more weight with C programmers starting out with Perl than saying, "Well the standard Perl idiom is..."

    Have you ever said, "This is Perl Style and easier to understand." and been told, "Well I am a C programmer, I have been using for loops for 15 years and they are easier for me to understand because I am used to them."? You won't win that argument...

      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)
        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://28478]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found