Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: 5x6-bit values into/out of a 32-bit word

by grinder (Bishop)
on Mar 18, 2007 at 10:28 UTC ( #605342=note: print w/ replies, xml ) Need Help??


in reply to Re^2: 5x6-bit values into/out of a 32-bit word
in thread 5x6-bit values into/out of a 32-bit word

The C programmer in you may rail against the inefficiency of your first solution, but this solution has you doing a pile of work that would be more efficiently handled by the computer. Get it to calculate those multiples of 6, it's better at that than you are. Factor that out, and your code will shrink.

And should the requirements ever change, you'll have much less code of your own to change. That's a much better form of effciency worth seeking.

• another intruder with the mooring in the heart of the Perl


Comment on Re^3: 5x6-bit values into/out of a 32-bit word
Re^4: 5x6-bit values into/out of a 32-bit word
by BrowserUk (Pope) on Mar 18, 2007 at 14:20 UTC
    Get [the computer] to calculate those multiples of 6, it's better at that than you are.

    What utter rubbish! If you cannot recite the first five terms of the 6 times table from memory, and more quickly than you can power up your computer...


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Don't take things so literally.

      Of course we know our 6 times tables. And the computer is already switched on.

      What if your requirements change, and it's 7 4-bit values, or 10 3-bit values? In my code, you only have to change one or two initialisers. In Thelonius's code you have to rework nearly all of it. Even if the particular edits are trivial, there are far too many.

      It's a variant on the Don't Repeat Yourself principle. I am surprised you didn't grasp this.

      • another intruder with the mooring in the heart of the Perl

        That's all very well, but there is no context upon which you could conclude that there is any merit whatsoever in prematurely generalising the stated problem.

        And in context, which as the writer of the OP I am uniquely qualified to judge, the is no chance that the requirements will ever change from 5x6-bit values. None.

        The underlying system that the problem models has been unchanged for at least 500, likely 600 and possibly 700 years. It ain't gonna change now, so the misbegotten computer pseudoscience of premature generalisation will achieve exactly naught.

        With the greatest of respect to my countryman, his sweeping generalisation of 40-odd years ago has been misused and abused so frequently in the intervening period, to the point where I think that outside of the rarefied atmosphere of academia and pure research, it's true meaning has been lost.

        I contend that in the modern world of commercial development, more money has been spent and wasted; more God-object hierarchies have been developed and discarded; and more development time wasted in pursuit of the premature generalisation than was ever the case with premature optimisation up to the time when he draughted his dictum.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^4: 5x6-bit values into/out of a 32-bit word
by Thelonius (Curate) on Mar 18, 2007 at 15:01 UTC
    This is a very interesting point, one very much worth considering. In Perl, of course, the difference would not be worth mentioning, but in C, my unrolled-loop version is much faster than a version with a loop. I was guessing that it would be twice as fast, but when I actually measured it, it was actually five times as fast.

    Now, of course, it was microseconds v. microseconds, but why are we packing 5 numbers into one 32-bit word? Presumably we care about the space usage, which would only matter if we are using a lot of them, probably millions, so all those microseconds can add up.

    The maintenance concerns are, in this specific case, probably not valid. You can't pack 6x6-bit values in a 32-bit word, nor 5x7-bit values. We would have to change the algorithm if anything changed. And, if you actually write out the loop version, you've probably only saved one line of code.

    I'm not disagreeing with your principles, but I think that in this case I would probably go with my version.

    There's a very good essay, The Fallacy of Premature Optimization. One snippet:

    Note, however, that Hoare did not say, "Forget about small efficiencies all of the time." Instead, he said "about 97% of the time." This means that about 3% of the time we really should worry about small efficiencies. That may not sound like much, but consider that this is 1 line of source code out of every 33. How many programmers worry about the small efficiencies even this often? Premature optimization is always bad, but the truth is that some concern about small efficiencies during program development is not premature.
      In Perl, of course, the difference would not be worth mentioning, but in C, my unrolled-loop version is much faster than a version with a loop.

      Yes, but this is Perl we are talking about, a language where a cosine is really no more expensive than an addition, not C. Perl is about flexibility, C is about speed.

      if we are using a lot of them, probably millions

      If you have millions of them, then Perl is the wrong language to use.

      All I know is that seeing those multiples of 6 raises a red flag, at least as far as Perl is concerned. That code strikes me as fragile, in that it does not adapt to changing requirements readily (which is one of the reasons I code in Perl). Remember the cardinal virtue of Laziness.

      Also, while I don't know why BrowserUK wants to do this, I maintain my reasoning is as valid as yours. There are dozens of ways of packing small bitmaps into a 32-bit quantity, and far more can be packed into a 64-bit quantity, as 64-bit CPUs become more prevalent.

      • another intruder with the mooring in the heart of the Perl

      Note, however, that Hoare did not say, "Forget about small efficiencies all of the time." Instead, he said "about 97% of the time." This means that about 3% of the time we really should worry about small efficiencies.

      As far as I knew, Hoare (implictly) did say "all of the time." The actual quotation being simply and forever:

      Premature optimization is the root of all evil.

      it was Knuth who paraphrased Hoare like thus:

      We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2014-07-14 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (255 votes), past polls