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

In reply to (jcwren) RE: (128?) C Style For and Foreach The Same by jcwren
in thread Finding the length of an array of arrays by target

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.