While I agree with what you want people to do, I think you have hit on the single worst possible reason to do it.

Namely code tuning.

There are far better reasons that you can and should point out. For instance one of the most common errors in C is a fencepost error. If you use C-style loops, it will be as common in Perl as it is in C. (In fact you have a fencepost error in your benchmark code.) If you loop using Perlish foreach loops, this error practically vanishes.

So by using foreach style looping you can kill one of your most common bugs. Not bad.

As for the abuses of map and grep, yes, they waste time and memory. However they also make your code less clear. They do more than a foreach loop does, and therefore you have more to think about when you run across one. That for me is a bigger deal than code tuning.

Oh, and a note. People say that they use map in golf because it is shorter. Wrong.

map$_++,@_; $_++for@_;
Even for golfing purposes, gratuitous abuse of map and grep is usually misguided.

Now if you can write code using a hash lookup instead of a grep, well that is not just code tuning, that is an algorithmic improvement. When you start talking algorithmic improvements, you get huge performance increases. But even so a hash lookup is clearer. So even if the program will run fast enough either way, I would use the hash lookup for clarity.

And everything that I just said about writing using Perl's syntactic sugar is sufficient for me to use it, regardless of whether it was faster. And it is important to think that way. Because if you talk to people about how fast constructs are, and teach them to think at that level, before you know it they will miss the forest for the trees.

Why would any performance oriented person use a hash instead of an array for a structure? Array access uses less memory and runs faster! But a hash is self documenting. It is faster to debug. You make fewer mistakes. What it costs in computer time and energy is more than made up for in human time.

I want people to use hashes. I want that because I don't want to waste my time wading through buggy and unreadable positionally based logic. And it doesn't happen until programmers understand that there is such a thing as "fast enough" and from then on their time is worth more than the computer's time. (Besides which, worrying about maintainability gives you more leisure to profile and find bigger speed increases later. Worrying about speed at every step is likely to result in a slower program in the end.)


In reply to Re (tilly) 1: Efficient Looping Constructs by tilly
in thread Efficient Looping Constructs by demerphq

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.