Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello QuillMeantTen, and welcome to the Monastery!

Since this is a learning exercise, I want to comment on your use of C-style for loops. For example, from sub heapify:

for(my $i = $start; $i < leonardo($order);$i++){ print "$$tab_ref[$i]\n"; }

As a general rule, whenever you have a C-style for loop in which the reinitialisation expression increments a variable by one, you should re-cast it as a Perl-style foreach loop instead:

for my $i ($start .. leonardo($order) - 1) { print "$$tab_ref[$i]\n"; }

Or, even better, put it on a single line by making the foreach a statement modifier:

print "$$tab_ref[$_]\n" for $start .. leonardo($order) - 1;

— which has the added advantage of simplifying the code by removing a variable declaration.

Not only is a Perl foreach loop intrinsically faster than its C-style equivalent; in this case you get a significant extra benefit in efficiency, because the subroutine call to leonardo($order) is made only once. Here’s a longer example to emphasise the point:

use strict; use warnings; my $x = 5; for (my $i = 0; $i < bar($x); $i++) { print "for loop: $i\n"; } print "----------\n"; for my $j (0 .. bar($x) - 1) { print "foreach loop: $j\n"; } sub bar { my $y = shift; print "bar($y)\n"; return $y; }

Output:

19:23 >perl 1378_Smoothsort.pl bar(5) for loop: 0 bar(5) for loop: 1 bar(5) for loop: 2 bar(5) for loop: 3 bar(5) for loop: 4 bar(5) ---------- bar(5) foreach loop: 0 foreach loop: 1 foreach loop: 2 foreach loop: 3 foreach loop: 4 19:23 >

Of course, this works only because the value returned by leonardo($order) doesn’t change (because $order isn’t changed in the body of the loop).

Another point to note: recursive functions like sub leonardo can often be significantly streamlined by memoization. This is explained in Chapter 3 of Higher-Order Perl by Mark Jason Dominus, which is available for free from http://hop.perl.plover.com/. See also the CPAN module Memoize (also by MJD).

Update: Fixed off-by-one error in foreach loops, thanks to AnomalousMonk (twice!).

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Smoothsort by Athanasius
in thread Smoothsort by QuillMeantTen

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (8)
    As of 2020-11-27 00:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?